您的位置:首页 > 其它

类 对象生存周期

2016-09-16 16:58 225 查看
类 对象生存周期

/*************************20160915C++*************************/
#include<iostream>
using namespace std;
/*对象生存周期
c
<=4  eax
>4 <=8 eax edx

返回对象,总产生临时量
C++ 空   1

*临时对象不在赋值构造同类型新对象时产生

*/

class Test
{
public:
Test(int m=10)
{
//cout << this << endl;
cout << "Test(int)" << endl;
ma = m;
}

~Test(){ cout << "~Test()" << endl; }
Test(const Test &res)
{
cout << "Test(const Test &)" << endl;
}
Test operator=(const Test &res)
{
cout << "operator=(const Test &)" << endl;
return *this;
}

int GetValue() { return ma; }

private:
int ma;
};
Test GetTest(Test &t)
{
int value = t.GetValue();
//Test tmp(value);
//return tmp;//对象 开辟内存,构造5
return Test(value);
}

int main()
{
Test t1(20);
//Test t2 = GetTest(t1);
/* Test t2;
t2 = GetTest(t1);*/
return 0;
}


Test operator=(const Test &res)

    {

        cout << "operator=(const Test &)" << endl;

        return *this;

    }

Test GetTest(Test &t)

{

    int value = t.GetValue();

    //Test tmp(value);

    //return tmp;//对象 开辟内存,构造5

    return Test(value);

}

Test operator=(const Test &res)

    {

        cout << "operator=(const Test &)" << endl;

        return *this;

    }

Test GetTest(Test &t)

{

    int value = t.GetValue();

    Test tmp(value);

    return tmp;//对象 开辟内存,构造5

    //return Test(value);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: