Dissecting Code

Wednesday, July 14, 2010

C++ variable assignment

Consider the following:

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: ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]



<< Home