C++编码练习3
1.关于构造函数。
class CA
{
public:
CA ():m_iData(0)
{
}
CA(int a)
{
m_iData =a;
}
CA (const CA& _obj)
{
m_iData =_obj.m_iData;
}
CA& operator = (const CA& _cObj)
{
m_iData = _cObj.m_iData;
return *this;
}
~CA()
{
}
protected:
private:
int m_iData;
};
int _tmain(int argc, _TCHAR* argv[])
{
CA _t1;//1
CA _t7 = CA();//2
CA _t2(3);//3
CA _t4 = 4;//4
CA _t5 = CA (5);//5
CA _t3(_t1);//6
CA _t6 = _t1;//7
_t6 = _t1;//8
return 0;
}
1和2调用:
CA ():m_iData(0)
{
}
3,4,5调用:
CA(int a)
{
m_iData =a;
}
6,7调用:
CA (const CA& _obj)
{
m_iData =_obj.m_iData;
}
8调用:
CA& operator = (const CA& _cObj)
{
m_iData = _cObj.m_iData;
return *this;
}
其中2,4,5没有调用拷贝构造函数是因为编译器进行了优化。
其中7于8的区别个人觉得是因为8中_t6 是已被声明。
参考:
http://cache.baiducontent.com/c?m=9f65cb4a8c8507ed4fece76310499c711923dd226d8d8b492cc3933fcf331d5c1331a3ee60630d548d98297a02af4f5cebf722376a4376b8cadf883acabbe23f2ff93044040bf63705a31ab8bb4032c050872ae2b869ecad863484d9a3&p=9139d416d9c133ff57ed95754852cc&newp=9c74c64ad4845fff57ed95754e4acd231610db2151d2d41438ca&user=baidu&fm=sc&query=C%2B%2B+%BF%BD%B1%B4%B9%B9%D4%EC&qid=eac3aa4b0001afe4&p1=4&fast=y