- class CTest
- {
- public:
- CTest(int iValue)
- {
- m_iValue = iValue;
- cout<<"Constructor Is Callled."<<endl;
- }
- ~CTest()
- {
- }
- CTest(CTest const& t)
- {
- m_iValue = t.m_iValue;
- cout<<"Copy Constructor Is Called."<<endl;
- }
- CTest& operator=(CTest const& test)
- {
- cout<<"重载赋值运算符."<<endl;
- m_iValue = test.m_iValue;
- return *this;
- }
- int GetValue() const
- {
- return m_iValue;
- }
- private:
- int m_iValue;
- };
- void printTest(CTest t)
- {
- cout<<t.GetValue()<<endl;
- }
- int main()
- {
- CTest t1(3);
- CTest t2(t1);
- t2 = t1; //会调用赋值构造函数
- CTest t3 = t1;//会调用拷贝构造函数
- printTest(t1); //会调用拷贝构造函数(和CTest t=t1 效果是一样的)
- return 0;
- }