class Test{
public:
    int a;
    Test(){
        a = 1;
    }
};

int main1()
{
    Test* t1 = new Test();
    t1->a = 10;
    
    Test* t2 = new Test();
    t2->a = 5;  
   
    t2 = t1; //t2原始值丢失,和t1指向同一个地方了,  
    
    t1->a = 111;//t2->a也变成了111,因为指向同一个地方了
    t2->a = 222;//t1->a也变成了222,因为指向同一个地方了 
}
int main2()  
{  
    Test t1;  
    t1.a = 10;  
      
    Test t2;  
    t2.a = 5;       
     
    t2 = t1;  //t1 t2 的a想等,把t1复制了一份
   
    t1.a = 111;  //不影响t2的值
    t2.a = 222;  //不影响t1的值   
}

 

posted on 2015-02-10 23:04  lypzxy  阅读(157)  评论(0编辑  收藏  举报