How operator overloading in c++ works -
i going through of theory of assignment operator of c++.
let's say
class myclass { private: t1 member1; t2 member2; public: // default copy assignment operator assigns object via memberwise copy myclass & operator=(const myclass & rhs) { member1 = rhs.member1; member2 = rhs.member2; return *this; } ...... }
and
c7 = c6; //member wise copy assignment.
here returning reference object during assignment operation , assigning new object c7
.
however if code this:
int a=12; int &b=a; int c=&b; //error::invalid conversion ‘int*’ ‘int’
why different above case??
the declaration
int& b = a;
declares b
reference, , makes reference variable a
.
the declaration
int c = &b;
declares c
normal int
variable, , try initialize pointer int
. ampersand &
different things depending on context.
as copy-assignment operator, it's not called when initialize variable in declaration, instead it's copy-constructor being called.
when have copy-assignment operator, , e.g.
myclass a, b; = b; // copy-assignment operator called
the compiler replaces assignment following call
a.operator=(b);
in other words, it's normal member-function call, other.
Comments
Post a Comment