s OVERLOADING THE ASSIGNMENT OPERATOR in Software

Make Data Matrix ECC200 in Software s OVERLOADING THE ASSIGNMENT OPERATOR
s OVERLOADING THE ASSIGNMENT OPERATOR
Data Matrix Creator In C#
Using Barcode generator for .NET framework Control to generate, create Data Matrix ECC200 image in Visual Studio .NET applications.
In this book we usually use the assignment operator as if it were a void function However, the prede ned assignment operator returns a reference that allows for some specialized uses With the prede ned assignment operator, you can chain assignment operators as follows: a = b = c;, which means a = (b = c); The rst operation, b = c, returns the new version of b So, the action of
Generate ECC200 In VS .NET
Using Barcode creator for ASP.NET Control to generate, create DataMatrix image in ASP.NET applications.
a = b = c;
Data Matrix 2d Barcode Encoder In VS .NET
Using Barcode drawer for .NET framework Control to generate, create Data Matrix image in Visual Studio .NET applications.
is to set a as well as b equal to c To ensure that your overloaded versions of the assignment operator can be used in this way, you need to de ne the assignment operator so it returns something of the same type as its left-hand side As you will see shortly, the this pointer will allow you to do this (No pun intended) However, while this requires that the assignment operator return something of the type of its left-hand side, it does not require that it return a reference Another use of the assignment operator explains why a reference is returned The reason that the prede ned assignment operator returns a reference is so that you can invoke a member function with the value returned, as in
DataMatrix Generator In Visual Basic .NET
Using Barcode generator for Visual Studio .NET Control to generate, create Data Matrix ECC200 image in .NET applications.
(a = b)f( );
Code 3 Of 9 Maker In VS .NET
Using Barcode generator for ASP.NET Control to generate, create Code 39 Extended image in ASP.NET applications.
where f is a member function If you want your overloaded versions of the assignment operator to allow for invoking member functions in this way, then you should have them return a reference This is not a very compelling reason for returning a reference, since this is a pretty minor property that is seldom used However, it is traditional to return a reference, and it is not signi cantly more dif cult to return a reference than to simply return a value For example, consider the following class (which might be used for some specialized string handling that is not easily handled by the prede ned class string):
Creating Bar Code In Visual Studio .NET
Using Barcode creation for VS .NET Control to generate, create bar code image in .NET applications.
Pointers and Dynamic Arrays
ANSI/AIM Code 39 Generation In VB.NET
Using Barcode creator for .NET Control to generate, create Code-39 image in VS .NET applications.
class StringClass { public: void someProcessing( ); StringClass& operator=(const StringClass& rtSide); private: char *a;//Dynamic array for characters in the string int capacity;//size of dynamic array a int length;//Number of characters in a }; = must be a member
UCC.EAN - 128 Creation In .NET Framework
Using Barcode creator for VS .NET Control to generate, create EAN / UCC - 13 image in .NET applications.
As noted in 8, when you overload the assignment operator it must be a member of the class; it cannot be a friend of the class That is why the above de nition has only one parameter for operator For example, consider the following:
Encode Bar Code In VB.NET
Using Barcode creator for .NET Control to generate, create bar code image in VS .NET applications.
s1 = s2;//s1 and s2 in the class StringClass
Making ECC200 In Java
Using Barcode generation for Java Control to generate, create Data Matrix image in Java applications.
calling object for =
ECC200 Reader In VS .NET
Using Barcode recognizer for VS .NET Control to read, scan read, scan image in .NET framework applications.
In the above call, s1 is the calling object and s2 is the argument to the member operator = The following de nition of the overloaded assignment operator can be used in chains of assignments like
Paint Code39 In C#
Using Barcode printer for VS .NET Control to generate, create Code-39 image in .NET applications.
s1 = s2 = s3;
Encoding Code 128 Code Set A In Visual Studio .NET
Using Barcode generator for ASP.NET Control to generate, create Code 128 Code Set B image in ASP.NET applications.
and can be used to invoke member functions as follows:
Creating GTIN - 13 In Visual Studio .NET
Using Barcode generation for ASP.NET Control to generate, create EAN13 image in ASP.NET applications.
(s1 = s2)someProcessing( );
UPC-A Supplement 5 Printer In .NET Framework
Using Barcode generator for .NET framework Control to generate, create GTIN - 12 image in Visual Studio .NET applications.
The de nition of the overloaded assignment operator uses the this pointer to return the object on the left side of the = sign (which is the calling object):
Generating Bar Code In Visual C#
Using Barcode maker for .NET Control to generate, create bar code image in .NET applications.
//This version does not work in all cases StringClass& StringClass::operator=(const StringClass& rtSide) { capacity = rtSidecapacity; length = rtSidelength; delete [] a; a = new char[capacity]; for (int i = 0; i < length; i++) a[i] = rtSidea[i]; return *this; }
Generate Data Matrix In .NET Framework
Using Barcode generator for .NET Control to generate, create Data Matrix 2d barcode image in VS .NET applications.
This version has a problem when used in an assignment with the same object on both sides of the assignment operator, like the following:
Encoding UCC.EAN - 128 In .NET
Using Barcode generator for ASP.NET Control to generate, create EAN / UCC - 13 image in ASP.NET applications.
s = s;
Encode Barcode In Visual Studio .NET
Using Barcode creator for ASP.NET Control to generate, create barcode image in ASP.NET applications.
Classes, Pointers, and Dynamic Arrays
Paint Barcode In C#
Using Barcode creator for .NET framework Control to generate, create bar code image in Visual Studio .NET applications.
When this assignment is executed, the following statement is executed:
delete [] a;
But the calling object is s, so this means
delete [] sa;
The pointer sa is then unde ned The assignment operator has corrupted the object s and this run of the program is probably ruined For many classes, the obvious de nition for overloading the assignment operator does not work correctly when the same object is on both sides of the assignment operator You should always check this case and be careful to write your de nition of the overloaded assignment operator so that it also works in this case To avoid the problem we had with our rst de nition of the overloaded assignment operator, you can use the this pointer to test this special case as follows:
//Final version with bug fixed: StringClass& StringClass::operator=(const StringClass& rtSide) { if (this == &rtSide) //if the right side is the same as the left side { return *this; } else { capacity = rtSidecapacity; length = rtSidelength; delete [] a; a = new char[capacity]; for (int i = 0; i < length; i++) a[i] = rtSidea[i]; return *this; } }