Bloomberg LP Interview Question
Financial Software DevelopersYou are right. It's two.
b=*a should be assignment operator.
Not sure how you thought it could be 3?
I used the following class to test.
The output of above code is:
Default Ctor
Copy Ctor
Copy Ctor
Assignment Operator
Copy Ctor
No sure why.
class A{
public:
A(int ii=0);
A(const A& a);
A operator=(const A& a);
//int get() const;
private:
int i;
};
A::A(int ii){
i=ii;
cout<<"Default Ctor"<<endl;
}
A::A(const A &a){
i=a.i;
cout<<"Copy Ctor"<<endl;
}
A A::operator =(const A &a){
i=a.i;
cout<<"Assignment Operator"<<endl;
return *this;
}
Because you are returning by value in assignment operator tahn by reference
Ur assignment operator shud be like this
A& A::operator =(const A &a){
i=a.i;
cout<<"Assignment Operator"<<endl;
return *this;
}
The code should only call 2 copy ctors once ur assignment operator is fixed
A* a =new A();//default constructor
- beyondfalcon August 21, 2010foo(*a); //copy constr
A b=*a; //copy constr
b=*a; //assignment constr