类对象的生存周期

让我们来从代码中看下对象的生存周期:

class Test
{
    public:
        Test(int a=10, int b=10)
        {
            ma = a;
            mb = b;
            cout<<"ma:"<<ma<<" mb:"<<mb<<endl;
            cout<<"Test(int) "<<this<<endl;

        }
        Test(const Test &src)
        {
            ma = src.ma;
            mb = src.mb;
            cout<<"ma:"<<ma<<" mb:"<<mb<<endl;
            cout<<"Test(const Test&) "<<&src<<"->"<<this<<endl;
        }

        void operator=(const Test &src)
        {
            ma = src.ma;
            mb = src.mb;
            cout<<"ma:"<<ma<<" mb:"<<mb<<endl;
            cout<<"operator=(const Test&) "<<&src<<"->"<<this<<endl;
        }

        ~Test()
        {
            cout<<"ma:"<<ma<<" mb:"<<mb<<endl;
            cout<<"~Test() "<<this<<endl;
        }
        void Show(){cout<<"ma:"<<ma<<" mb:"<<mb<<endl;}
        int GetValue1(){return ma;}
        int GetValue2(){return mb;}
    private:
        int ma;
        int mb;
};

//返回一个对象,会产生临时对象,因为带回一个对象,要先构造
Test GetObject(Test t)
{
    int a = t.GetValue1();
    int b = t.GetValue2();
    Test tmp(a,b);
    //tmp拷贝构造临时对象,函数调用之前产生临时对象  临时量的地址    ebp+8(最后入栈的)
    return tmp;
}
int main()
{
    Test t1(10, 10);
    Test t2;
    //   内存(放临时对象)  传入临时量地址  GetObject(void*, t1) 
    t2=GetObject(t1);  // func()  ->   func(void*)
    cout<<"-----------------"<<endl;
    t2.Show();
    return 0;
}
main()函数中类对象的生存周期:先构造t1,再构造t2, t1拷贝构造给t ,构造tmp,tmp拷贝构造临时变量,析构tmp,析构t,临时变量赋值给t2,析构临时变量,析构t2 t1

但上述代码仍需继续优化,将其GetObject()函数稍作改变:

//这里用引用优化了参数的生成,没有实参拷贝构造形参,也少了形参的析构
Test GetObject(Test &t)
{
    int a = t.GetValue1();
    int b = t.GetValue2();
    //返回正在生成的对象/临时对象  因为这里是用临时对象生成一个新对象,所以不产生临时变量(直接构造临时对象)
    return Test(a, b);
}

int main()
{
    Test t1(10, 10);
     Test t2;
    //   内存(放临时对象)    GetObject(void*, t1) 
    t2=GetObject(t1);
    t2.Show();
    return 0;
}

这里的main()函数先构造t1 t2,子函数产生返回临时变量,临时变量赋值给t2,析构临时变量,析构t2 析构t1,是不是和上一步相比少了些许构造和析构呢?答案是肯定的。

然后我们还可以进一步优化:

Test GetObject(Test &t)
{
    int a = t.GetValue1();
    int b = t.GetValue2();
    return Test(a, b);//返回正在生成的对象 

int main()
{
    Test t1(10, 10);

    //   内存(放临时对象)    GetObject(void*, t1) 
    Test t2=GetObject(t1);//直接定义并初始化

    t2.Show();

    return 0;
}

当我们在主函数中直接定义并初始化的时候,与上一步相比,少了一次构造和赋值,也少了一次析构。因为这里是临时对象构造一个新的对象,临时对象被优化。相当于在返回的时候直接就构造了t2。

让我们接下来看另一个例子:

class Cgoods
{
  public:
        //直接构造--初始化
        Cgoods(char *n,int m,float p);
        //拷贝构造函数 ,按引用接收,如果是值将进入死循环
        Cgoods(const  Cgoods &src);
        //赋值运算符重载--赋值运算符可以是引用或传递值,若是引用会减少形参对象的生成
        void operator=(const  Cgoods &src);
        //析构函数--释放占用的外部资源
        ~Cgoods();

  private:
        char* mName;
        int mAmount;
        float price;
};

Cgoods ::Cgoods(char *n,int m,float p)
{
    cout<< this<<endl;
    cout<< "Cgoods(char *n,int m,float p)"<<endl;
     mName=new [strlen(n)+1];
     strcpy(mName,n);
     mAmount=m;
     price=p;

}

Cgoods ::Cgoods(const  Cgoods &src)
{
    cout<<&src<<"->"<< this<<endl;
    cout<<"Cgoods(const  Cgoods &src)"<<endl;
    mName=new [strlen(src.mName)+1];
    strcpy(mName,src.mName);
    mAmount=src.mAmount;
    price=src.price;
}

void Cgoods ::operator=(const  Cgoods &src)
{
    cout<<&src<<"->"<< this<<endl;
    cout<<"operator=(const  Cgoods &src)"<<endl;

    if (&src==this)//防止自赋值
    {
        return ;
    }

    delete []mName;//释放原来的资源
    mName=NULL;


    mName=new [strlen(src.mName)+1];
    strcpy(mName,src.mName);
    mAmount=src.mAmount;
    price=src.price;
}

Cgoods::~Cgoods()
{
    delete []mName;
    mName=NULL;
}
 void fun(Cgoods *pgood)
 {
    cout<<"call func"<<endl;
 }
int main()
{
     //先构造--显式生成临时变量---传参--打印call func---调用析构函数
     //主要是看临时对象的生存周期(在这个函数调用完成之后临时对象将不存在)
      func(Cgoods ("shangpin1",14,14.1));

    //先构造的后析构
    //构造函数在对象定义时产生汇编
    Cgoods good1("shangpin1",14,14.1);
    Cgoods good2(good1);//拷贝构造


    //先构造带三个参数的构造函数然后直接构造good3

     Cgoods("shangpin1",15,15.1)//显示生成临时对象
    //当一个临时对象构造一个新对象的时候,临时对象会被优化
    Cgoods good3=Cgoods("shangpin1",15,15.1);


    //先构造带三个参数的构造函数,显式生成临时对象,将临时对象赋值构造给good3,析构临时变量
    //下面这两种都是显式生成临时对象,当只有一个参数时,这两种函数等价,有多个参数时,不等价
    good3=Cgoods("shangpin1",16,16.1);
    good3=(Cgoods)("shangpin1",16,16.1);//类型转化  逗号表达式  将double类型转化为Cgoods,寻找带有该类型的构造函数


    //隐式生成临时对象,将临时对象赋值给good3,析构隐式生成临时对象
    good3=40.1;

    //不管是显式生成临时对象还是隐式生成临时对象,初始化不产生临时对象,赋值才会产生临时对象
    Cgoods good4=60.8;


    //临时对象的生存周期是其所在的语句,或是所在函数的调用结束(不包括引用变量引用的临时对象)
    //这里的good5之所以析构是临时对象的生存周期结束
    //指针有自己独立的内存,而引用没有自己对立的内存。引用就像一块橡皮糖,引用变量所引用的临时量的生存周期是引用变量的生存周期,也就是这个临时对象的生存周期
    Cgoods *good5=&Cgoods("shangpin1",17,17.1);//先构造--产生临时对象--析构临时对象
    Cgoods &good6=Cgoods("shangpin1",18,18.1);//先构造--产生临时对象---这里先不析构

    return 0;//析构函数产生
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值