对象作为函数参数时,实参初始化形参的时候调用拷贝构造函数
#include <iostream>
using namespace std;
class Locaton
{
public:
Locaton(int xxx = 0, int yy = 0)
{
X = xx;
Y = yy;
cout << "constructor Object!" << endl;
}
Locaton(const Locaton &p)
{
X = p.x;
Y = p.y;
cout << "Cpoy constructor called!" << endl;
}
~ Locaton()
{
cout << X << "," << "Object destoryed!" << endl;
}
int GetX()
{
return X;
}
int GetY()
{
return Y;
}
private:
int X;
int Y;
};
void f(Locaton p)
{
cout << "Function:" << p.GetX() << "," << p.GetY() << endl;
}
void mainobjplay()
{
Locaton A(1, 2);
f(A);//A实参初始化形参p,会调用拷贝构造函数
}
void main(int argc, char const *argv[])
{
mainobjplay();
return 0;
}