袁辉勇 4=27-4 拷贝构造函数的调用 P184

1==   T t2(t1);     {  T t3=t2; }

#include <iostream>
using namespace std;
class T
{
     int x;
public:
    T(int xx) {  x=xx;      cout<<"constructor: "<<x<<endl;  }
    ~T( )     {             cout<<"destructor: " <<x<<endl;  }
    
    T(T &t) {  x=t.x+99;    cout<<"copy constructor: "<<x<<endl;  }
};


int main()
{    
    T t1(10);
    T t2(t1);
    {     T t3=t2;    }
    cout<<"****************************\n";     
}






2==   void disp(T t)  {    cout<<t.get_x()<<endl;}                   disp(t1);
#include <iostream>
using namespace std;
class T
{    int x;
public:
      T( int xx )    {  x=xx; cout<<"constructor: "<<x<<endl;  }
    ~T( )      {   cout<<"destructor: "<<x<<endl;  }
    T(T &t)  {  x=t.x+99; cout<<"copy constructor: "<<x<<endl;  }
    int get_x()      {      return x;}
};

void disp(T t)
{    cout<<t.get_x()<<endl;}

int main( )
{    T t1(10);
    disp(t1);
    cout<<"****************************\n";    
}









#include <iostream>
using namespace std;
class T
{
    int x;
public:
    T(int xx)        {  x=xx;         cout<<"constructor: "<<x<<endl;          }
    ~T( )                {              cout<<"destructor: "<<x<<endl;          }

    T(T &t)     {  x=t.x+99;   cout<<"copy constructor: "<<x<<endl;  }
    
    int get_x()        {      return x;}
};


T f( )
{    T t(100);  
      return t;    }

int main()
{
    T t1(10);
    t1=f();
    cout<<"****************************\n";    
}









C++ 文章被收录于专栏

C++谭浩强 冯博强 贾应知 袁辉勇

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务