Assignment Operator Overload in c++ -


i realize there examples after examples of overloading assignment operator on web, have spent last few hours trying implement them in program , figure out how work , can't seem , appreciated.

i trying implement overloaded assignment operator function.

i have 3 files working, complex.h header file, complex.cpp definition file, , .cpp file i'm using driver test complex class.

in complex.h header file prototype assignment operator:

complex &operator= (complex&); 

and far have definition of overloaded operator in .cpp file is:

    complex &complex::operator=(complex& first)     {          if (real == first.real && imaginary == first.imaginary)              return complex(real, imaginary);           return first;     }; 

and way i'm calling assignment operator in functions is:

   x = y - z; 

so specifically, problem when call overloaded assignment operator x = y -z, doesn't assign passed in value x when return passed in value , i'm unable figure out why numerous examples , explanations on web, appreciated , thank ahead of time.

i think need following operator definition

complex & complex::operator =( const complex &first ) {      real = first.real;      imaginary = first.imaginary;       return *this; }; 

you have return reference object assigned to. parameter should constant reference. in case may bind reference temporary object or have write move assignment operator.

in implementation of copy assignment operator not change assigned object.:) create temporary , return reference temporary or return reference first.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -