C++_类和对象3_对象的生存周期

定义一个class类:
class Test
{
public:
    Test(int a, int b)
    {
        std::cout << this << " :Test::Test(int,int)" << std::endl;
        ma = a;
        mb = b;
    }
    Test(int a)
    {
        std::cout << this << " :Test::Test(int)" << std::endl;
        ma = a;
        mb = 0;
    }
    Test()
    {
        std::cout << this << " :Test::Test()" << std::endl;
        ma = mb = 0;
    }
    Test(const Test& rhs)
    {
        std::cout << this << " :Test::Test(const Test&)" << std::endl;
        ma = rhs.ma;
        mb = rhs.mb;
    }
    Test& operator=(const Test& rhs)
    {
        std::cout << this << " :Test::operator=(const Test&)" << std::endl;
        if (this != &rhs)
        {
            ma = rhs.ma;
            mb = rhs.mb;
        }
        return *this;
    }
    ~Test()
    {
        std::cout << this << " :Test::~Test()" << std::endl;
    }
    int getValue()
    {
        return ma;
    }
private:
    int ma;
    int mb;
};

Test getObject1(Test& lhs)
{
    return lhs.getValue();              //隐式调用
    //return Test(lhs.getValue());   //显式调用
}

int main()
{
    Test test1(10, 20);
    Test rt;
    rt = getObject(test1);
std::cout << "------------------" << std::endl;

    Test test2 = 10;
    test2 = 30;
    std::cout << "------------------" << std::endl;
    
    Test test3(20, 30);
    (rt = test1) = test3;
    std::cout << "------------------" << std::endl;
    return 0;
}
问生成几个对象? 
实参传形参过程中,形参对象会开辟内存,构造生成对象、用引用接收实参,减少内存的开辟,减少构造函数和析构函数的调用
012FFC9C :Test::Test(int,int)                     调用两个参数的构造函数生成test1;
012FFC8C :Test::Test()                              调用无参数的构造函数生成rt;(用户不定义系统默认生成)
012FFB80 :Test::Test(int)                           调用一个参数的构造函数隐式生成临时对象;
012FFC8C :Test::operator=(const Test&)  调用赋值运算符的重载函数将隐式生成的临时对象赋值给rt
012FFB80 :Test::~Test()                             隐式调用生成的临时对象在表达式结束,生存周期结束,被系统调用析构函数销毁
------------------
012FFC7C :Test::Test(int)                          调用一个参数的构造函数生成test2
012FFB90 :Test::Test(int)                           int与Test类型不匹配,系统在类中寻找并调用一个参数的构造函数生成临时对象;
012FFC7C :Test::operator=(const Test&)   调用赋值运算符的重载函数将临时对象赋值给test2
012FFB90 :Test::~Test()                             临时对象在表达式结束,生存周期结束,被系统调用析构函数销毁
------------------
012FFC6C :Test::Test(int,int)                       调用两个参数的构造函数生成test3;
012FFC8C :Test::operator=(const Test&)    (rt = test1) = test3      等价于   (rt.operator=(test1)) = test2
012FFC8C :Test::operator=(const Test&)     调用两次赋值运算符的重载函数先将test1赋值给rt再将test2赋值给rt
------------------
012FFC6C :Test::~Test()                             test3生存周期结束,被系统调用析构函数销毁
012FFC7C :Test::~Test()                             test2生存周期结束,被系统调用析构函数销毁
012FFC8C :Test::~Test()                             rt生存周期结束,被系统调用析构函数销毁
012FFC9C :Test::~Test()                             test1生存周期结束,被系统调用析构函数销毁
 

对象的生存周期:
class CGoods
{
public:
    CGoods(char* name, float price, int amount)
    {
        std::cout << this << " :CGoods::CGoods(char*,float, int)" << std::endl;
        mname = new char[strlen(name) + 1]();
        strcpy(mname, name);
        mprice = price;
        mamount = amount;
    }
    CGoods(int amount)
    {
        std::cout << this << " :CGoods::CGoods(int)" << std::endl;
        mname = new char[1]();
        mamount = amount;
    }
    CGoods()
    {
        std::cout << this << " :CGoods::CGoods()" << std::endl;
        mname = new char[1]();
    }
    ~CGoods()
    {
        std::cout << this << " :CGoods::~CGoods()" << std::endl;
        delete[] mname;
        mname = NULL;
    }
    CGoods(const CGoods& rhs)
    {
        std::cout << this << " :CGoods::CGoods(const CGoods&)" << std::endl;
        mname = new char[strlen(rhs.mname) + 1]();
        strcpy(mname, rhs.mname);
        mprice = rhs.mprice;
        mamount = rhs.mamount;
    }
    CGoods& operator=(const CGoods& rhs)
    {
        std::cout << this << " :CGoods::operator=(const CGoods&)" << std::endl;
        if (this != &rhs)
        {
            delete[] mname;
            mname = new char[strlen(rhs.mname) + 1]();
            strcpy(mname, rhs.mname);
            mprice = rhs.mprice;
            mamount = rhs.mamount;
        }
        return *this;
    }
private:
    char* mname;
    float mprice;
    int mamount;
};
CGoods ggood1("good1", 10.1, 20);
int main()
{
    CGoods good3;
    CGoods good4(good3);

    good4 = good3;

    static CGoods good5("good5", 10.1, 20);

    CGoods good6 = 10;
    CGoods good7(10);
    CGoods good8 = CGoods("good8", 10.1, 20);

    good6 = 20;
    good7 = CGoods(20);
    good8 = (CGoods)("good8",10.1, 20);

    CGoods* pgood9 = new CGoods("good9", 10.1, 20);//heap
    CGoods* pgood10 = new CGoods[2];

    std::cout << "------------------" << std::endl;
    CGoods* pgood11 = &CGoods("good11", 10.1, 20);
    std::cout << "------------------" << std::endl;
    //CGoods* pgood12 = 20;错误int 与 *不能进行’=‘操作
    CGoods& rgood12 = CGoods("good11", 10.1, 20);
    std::cout << "------------------" << std::endl;
    const CGoods& rgood13 = 20;

    delete pgood9;
    delete[] pgood10;
    return 0;
}
CGoods ggood2("good2", 10.1, 20);//.data

00AB0230 :CGoods::CGoods(char*,float, int)        调用三个参数的构造函数生成ggood1;
00AB0220 :CGoods::CGoods(char*,float, int)        调用三个参数的构造函数生成ggood2;
00A4F984 :CGoods::CGoods()                              调用无参数的构造函数生成good3(或系统默认的构造函数);
00A4F970 :CGoods::CGoods(const CGoods&)     调用拷贝构造函数生成用good3生成good4;
00A4F970 :CGoods::operator=(const CGoods&)  调用赋值运算符的重载函数将good3赋值给good4;
00AB023C :CGoods::CGoods(char*,float, int)       调用三个参数的构造函数生成静态的ggood5;放在.data段,
00A4F95C :CGoods::CGoods(int)                         调用一个参数的构造函数生成ggood6
00A4F948 :CGoods::CGoods(int)                          调用一个参数的构造函数生成ggood7
00A4F934 :CGoods::CGoods(char*,float, int)        调用三个参数的构造函数生成ggood8,直接生成对象,优化不产生临时对象
00A4F754 :CGoods::CGoods(int)                          int、Test类型不匹配,系统寻找并调用一个参数的构造函数生成临时对象;
00A4F95C :CGoods::operator=(const CGoods&) 调用赋值运算符的重载函数将临时对象赋值给test6
00A4F754 :CGoods::~CGoods()                            临时对象在表达式结束,生存周期结束,被系统调用析构函数销毁
00A4F768 :CGoods::CGoods(int)                          调用一个参数的构造函数显示生成临时对象
00A4F948 :CGoods::operator=(const CGoods&)  调用赋值运算符的重载函数将临时对象赋值给test7
00A4F768 :CGoods::~CGoods()                            临时对象在表达式结束,生存周期结束,被系统调用析构函数销毁
00A4F77C :CGoods::CGoods(int)                         ’=‘(强转类型)(’,‘表达式)调用一个参数的构造函数显示生成临时对象
00A4F934 :CGoods::operator=(const CGoods&)  调用赋值运算符的重载函数将临时对象赋值给test8
00A4F77C :CGoods::~CGoods()                           临时对象在表达式结束,生存周期结束,被系统调用析构函数销毁
001FD410 :CGoods::CGoods(char*,float, int)       用new在堆区调用三个参数的构造函数创建函数对象,在栈区用指针指向该对象的地址区域;遇到delete时堆区对象删除
001FD714 :CGoods::CGoods()                              在堆区调用一个参数的构造函数生成CGoods对象,用*pgood10[0]指向
001FD720 :CGoods::CGoods()                              在堆区调用一个参数的构造函数生成CGoods对象,用*pgood10[1]指向
------------------
00A4F7C0 :CGoods::CGoods(char*,float, int)       显式调用三个参数的构造函数生成临时对象并用* pgood11指向
00A4F7C0 :CGoods::~CGoods()                           表达式结束,临时对象销毁
------------------
00A4F8F0 :CGoods::CGoods(char*,float, int)       显式调用三个参数的构造函数生成临时对象,用’&‘pgood12接受,’&‘提升临时对象的生存周期,将临时对象生存周期提升至main函数结束
------------------
00A4F8D0 :CGoods::CGoods(int)                          调用一个参数的构造函数生成rgood13,’&‘提升临时对象的生存周期,
001FD410 :CGoods::~CGoods()                             delete pgood9 函数调用析构函数销毁
001FD720 :CGoods::~CGoods()                             delete[] pgood10
001FD714 :CGoods::~CGoods()                             delete[] pgood10
00A4F8D0 :CGoods::~CGoods()                             main函数结束,系统调用析构函数销毁rgood13
00A4F8F0 :CGoods::~CGoods()                             系统调用析构函数销毁rgood12
00A4F934 :CGoods::~CGoods()                             系统调用析构函数销毁rgood8
00A4F948 :CGoods::~CGoods()                             系统调用析构函数销毁rgood7
00A4F95C :CGoods::~CGoods()                            系统调用析构函数销毁rgood6
00A4F970 :CGoods::~CGoods()                             系统调用析构函数销毁rgood4
00A4F984 :CGoods::~CGoods()                             系统调用析构函数销毁rgood3                    
00AB023C :CGoods::~CGoods()                            系统调用析构函数销毁rgood5
00AB0220 :CGoods::~CGoods()                             系统调用析构函数销毁rgood2
00AB0230 :CGoods::~CGoods()                             系统调用析构函数销毁rgood1

生存周期:
全局对象的生存周期:从程序运行开始,程序运行结束结束
静态局部对象的生存周期:调用点开始,函数结束结束
临时对象生存周期:调用点开始,表达式结束结束

临时量类型:
        1.内置类型生成临时量为常量
        2.自定义类型生成临时量为变量
        3.隐式调用生成的临时量为常量

​​​​​​临时对象的优化:
          临时对象的目的:为了生成新对象、以生成临时对象的方式生成新对象
          引用能提升临时对象的生存周期、把临时对象提升和引用变量相同的生存周期

 

 

 

 

 

 

 

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值