c++类默认拷贝构造函数---浅复制

类默认的拷贝构造函数是浅复制,比如如下程序,类中指向的地址是一样的

#include <iostream>
using namespace std;

class Test
{
public:
    int *a;
};

int main() 
{
    Test *t = new Test();
    int aa = 1314520;
    t->a = &aa;

    Test *t2 = new Test(*t);

    cout<<t->a<<endl;
    cout<<t2->a<<endl;

    return 0; 
}

你可能感兴趣的:(c++类默认拷贝构造函数---浅复制)