匿名對象的深入分析

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;


class Location
{
public:
Location(int xx = 0, int yy = 0)
{
X = xx;
Y = yy;
cout << "Constructor Object.\n";
}
Location(const Location &p)
{
X = p.X;
Y = p.Y;
cout << "Copy_constructor called." << endl;
}
~Location()
{
cout << X << "," << Y << " Object destroyed." << endl;
}
int getx()
{
return X;
}
int gety()
{
return Y;
}
private:


int X, Y;
};


void f(Location p)
{
cout << "Funtion:" << p.getx() << "," << p.gety() << endl;
}


Location g()
{
Location A(1, 2);
return A;
}
void mainobjplay()
{
//Location B;
//B = g();//40 =等號操作 


Location B = g();
f(B);
cout << "Funtion:" << B.getx() << "," << B.gety() << endl;

//42 對象初始化操作    直接將g()返回的匿名對象轉變爲B對象,匿名對象會新開闢新的內存空間,應該存在棧區,但不知道。
//如果返回的匿名對象,來初始化另外一個同類型的類對象,那麼匿名對象會直接轉成新的對象。。。
//匿名對象的去和留,關鍵看,返回時如何接過來。
cout << "測試測試" << endl;

}


void main()
{
mainobjplay();
system("pause");
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章