Huangxinyu
BAN USERExactly, thanks
- Huangxinyu June 23, 2010I 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;
}
Well, if I remember right, objects are always created on heap. Correct me if i am wrong
- Huangxinyu June 23, 2010My answer is 2. But I have tested it, the actual number should be 3. I understand it called copy ctor for foo(*a) and A b=*a;
But why does it call copy ctor after assignment operator of b=*a; Can some one explain plz?
I told him that they both called the default ctor. But the first one is dynamic allocating the memory which needs to be deleted manually. And you don't have to delete the memory for the second one. Any comments?
- Huangxinyu June 22, 2010linked list
Search the book, add a link when you come to a new chapter. every node contains chapter number page number, chapter name and abstract.
4/5 is right
3/4+1/4*1/5
Most of you guys are right.
- Huangxinyu June 25, 2010C wears black.