一点疑惑
有一段运行正常的程序是这样的:
例3-15:分析以下程序的执行结果
#include <iostream.h>
class Location {
public :
Location ( int xx=0, int yy=0 )
{ X=xx ; Y=yy;
cout << "Object constructed." << endl ; }
Location ( Location & p ) ;
~Location ( )
{ cout << X << "," << Y << "Object destroyed." << endl ; }
int GetX ( ) { return X ; }
int GetY ( ) { return Y ; }
private : int X , Y ;
} ;
Location :: Location ( Location & p )
{ X= p.X ; Y=p.Y ; cout << "Copy_constructor called." << endl ; }
Location g ( )
{ Location A ( 1 , 2 ) ; return A ; }//此处的A为局部变量为什么能返回呢?而且将调用拷贝构造函数??????
void main ( )
{ Location B ;
B = g ( ) ; } //给对象 B 赋值