C++ variable assignment
Consider the following:
1) A a; => Constructor is invoked
2) A b=a; => Copy constructor is invoked.
A c;
c=b; => Assignment operator is invoked.
A a=10; =>Constructor is invoked to create a temporary A object. Then a's copy constructor is invoked.
Source: Guru of the day 1
class A
{
int a;
public:
A(){a=0;}
A(int b):a(b){}
A(const A& a1){ a=a1.a; }
A& operator=( const A& a1){ a=a1.a; return *this; }
};
1) A a; => Constructor is invoked
2) A b=a; => Copy constructor is invoked.
A c;
c=b; => Assignment operator is invoked.
A a=10; =>Constructor is invoked to create a temporary A object. Then a's copy constructor is invoked.
Source: Guru of the day 1
Labels: assignment operator, titbits
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home