类和对象 02类对象的生存周期两个习题

本文深入探讨C++中构造函数与析构函数的使用,包括不同类型的构造函数如默认构造函数、拷贝构造函数、移动构造函数以及带有参数的构造函数,同时解析析构函数的工作原理。通过实例演示了对象生命周期中这些函数的调用顺序,帮助读者理解对象创建和销毁过程中的资源管理。

1.请给出下面对象创建过程中涉及的方法打印
{
public:
    Test(int a=5, int b=5):ma(a), mb(b)
    {cout<<"Test(int, int)"<<endl;}
    ~Test()
    {cout<<"~Test()"<<endl;}
    Test(const Test &src):ma(src.ma), mb(src.mb)
    {cout<<"Test(const Test&)"<<endl;}
    void operator=(const Test &src)
    {ma = src.ma; mb = src.mb; cout<<"operator="<<endl;}
private:
    int ma;
    int mb;
};
Test t1(10, 10);
int main()
{
    Test t2(20, 20);
    Test t3=t2;
    static Test t4 = Test(30, 30);
    t2 = Test(40, 40);
    t2 = (Test)(50, 50);
    t2 = 60;
    Test *p1 = new Test(70, 70);
    Test *p2 = new Test[2];
    Test *p3 = &Test(80, 80);
    Test &p4 = Test(90, 90);
    delete p1;
    delete []p2;
}
Test t5(100, 100);

答案:

打印结果:

Test(int, int)//调用带两个参数的构造函数生成t1对象
Test(int, int)//调用带两个参数的构造函数生成t5对象
Test(int, int)//调用带两个参数的构造函数生成t2对象
Test(const Test&)//调用拷贝构造函数生成t3对象
Test(int, int)//调用带两个参数的构造函数生成t4对象
Test(int, int)//调用带两个参数的构造函数显示生成t2临时对象 此时并没有新对象生成 无优化 调用临时对象的构造函数
operator=//调用赋值运算符重载函数
~Test()//遇上表达式结束 临时对象销毁 调用析构函数
Test(int, int)// 逗号表达式 取50 调用带两个参数(另一个参数为默认值)的构造函数显示生成t2临时对象 此时并没有新对象生成 无优化 调用临时对象的构造函数
operator=//调用赋值运算符重载函数
~Test()//遇上表达式结束 临时对象销毁 调用析构函数
Test(int, int)//调用带两个参数的构造函数隐式生成t2临时对象 此时并没有新对象生成 无优化 调用临时对象的构造函数
operator=//调用赋值运算符重载函数
~Test()//遇上表达式结束 临时对象销毁 调用析构函数
Test(int, int)//new创建了新对象 从new到delete结束 调用构造函数 p1
Test(int, int)//调用构造函数 new了一个数组 每个对象生成都会调用构造函数 所以这里调用两次构造函数 p2
Test(int, int)//同上 数组 p2
Test(int, int)//取了临时对象的地址赋给指针P3 指针不会提升临时对象生存周期 引用才会提升 调用构造函数 p3
~Test()//临时对象遇见表达式结束销毁 调用析构函数 p3
Test(int, int)//引用了这个临时对象 引用提升了临时对象的生存周期 此时只有临时对象的构造函数 析构暂时没有 p4
~Test()//new了就得delete 销毁P1所指向的内存 是对象内存 销毁资源 调用析构函数 p1
~Test()//p2指向了对象的数组 会调用两次析构函数
~Test()//同上 数组销毁 p2
~Test()//p4引用的临时对象销毁 调用析构函数
~Test()//t3销毁 调用析构函数
~Test()//t2销毁 调用析构函数
~Test()//t4销毁 调用析构函数
~Test()//t5销毁调用析构函数
~Test()//t1销毁 调用析构函数

 

2.请写出下面程序运行时方法涉及的打印信息
class Test
{
public:
    Test(int a=5):ma(a){cout<<"Test(int)"<<endl;}
    ~Test(){cout<<"~Test()"<<endl;}
    Test(const Test &src):ma(src.ma)
    {cout<<"Test(const Test&)"<<endl;}
    void operator=(const Test &src)
    {
        ma = src.ma;
        cout<<"operator="<<endl;
    }
    int GetValue(){return ma;}
private:
    int ma;
};
Test GetTestObject(Test &t)
{
    int value = t.GetValue();
    Test tmp(value);
    return tmp;
}
int main()
{
    Test t1(20);
    Test t2;
    t2 = GetTestObject(t1);
    cout<<t2.GetValue()<<endl;
    return 0;
}

答案:

打印结果:

Test(int)//调用一个整形参数的构造函数生成对象t1
Test(int)//调用一个整形参数(默认值)的构造函数生成对象t2
Test(int)//调用一个整形参数的构造函数生成tmp对象
Test(const Test&)//return时会有临时对象生成 拿一个已存在的对象生成新对象 调用拷贝构造函数生成临时对象
~Test()//tmp对象的销毁
operator=//拿临时对象给t2赋值 调用赋值运算符的重载函数
~Test()//临时对象销毁
20//打印t2 Getvalue的值
~Test()//析构t2对象销毁
~Test()//析构t1对象销毁
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值