Question:
Operator overloading in C++?
anonymous
2008-01-14 11:23:43 UTC
hi everyone..
can you please tell me how to make overloading for the operater =....
and explain if you can...
thanx
Four answers:
anonymous
2008-01-16 01:45:35 UTC
1- Since it is one of the assignment operators (=, +=, -=, etc.), it MUST be overloaded as a non-static (public) member function in your class, because it will operate on user-defined objects.

2- Considering the above and the fact that it is a binary operator, the assignment operator (=) will be overloaded with one argument.

3- So a call like a = b for example, will be treated as a.operator=(b), where a and b are objects of your class.

4- The following is an INCOMPLETE example from my book.** It is a user-defined "Array" class.

5- Defining a proper COPY CONSTRUCTOR is essential for using the overloaded assignment operator correctly.

6- Notice the use of the ampersand operator (&) to indicate "lvalues", and the use of "this" pointer to allow cascaded calls (e.g., a = b = c ).

7- I've enclosed links to download the COMPLETE version of this example. Below you'll find two links, one for the Array.h file and the other for the Array.cpp file. It is really a rich example to learn about operator overloading. Don't worry about copyrights, this example is completely FREE, says the author!



class Array

{

public:

Array ( int = 10 ); //default constructor

Array ( const Array & ); //copy constructor

const Array &operator= ( const Array & ); //overloaded (=)

private:

int size;

int *ptr;

}

----------------------------------------------------------------------

// overloaded assignment operator;

// const return avoids: ( a1 = a2 ) = a3

const Array &Array::operator=( const Array &right )

{

if ( &right != this ) // avoid self-assignment

{

// for Arrays of different sizes, deallocate original

// left-side array, then allocate new left-side array

if ( size != right.size )

{

delete [] ptr; // release space

size = right.size; // resize this object

ptr = new int[ size ]; // create space for array copy

} // end inner if



for ( int i = 0; i < size; i++ )

ptr[ i ] = right.ptr[ i ]; // copy array into object

} // end outer if



return *this; // enables x = y = z, for example

} // end function operator=

---------------------------------------------------------------------



Hope that helped!
?
2016-10-04 15:39:46 UTC
Operator overloading is under polymorphism a undeniable characteristic in C++. on an analogous time as the operators in C have universal meaning operator overloading in C++ enables us to overload the operator to offer the specific consumer defined function to the operator. Syntax is going like this operator x(arguments) { function } the place x is an operator operator overloading is quite often used to function the gadgets utilising operators quite of working their individuals.
CatNip
2008-01-14 12:06:40 UTC
class someClass

{

public:

...

someClass & operator=(const someClass &s);

...

}
BIG FOOT
2008-01-14 12:09:44 UTC
try this tutorial: http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=/com.ibm.xlcpp8l.doc/language/ref/cplr326.htm


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...