匿名对象的深入分析

来源:互联网 发布:郑州丰泽教育编程 编辑:程序博客网 时间:2024/05/01 21:08
#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");
}
0 0