#include<iostream>
using namespace std;
class Point{
public:
Point(int xx=0,int yy=0){X=xx;Y=yy;}
Point(const Point &p);
int GetX(){return X;}
int GetY(){return Y;}
private:
int X,Y;
};
Point::Point(const Point& p)
{
X=p.X;
Y=p.Y;
cout<<"拷贝构造函数调用"<<endl;
}
void fun1(Point p)
{
cout<<p.GetX()<<endl;
}
Point fun2()
{
Point A(1,2);
return A; /*当函数的返回值是类对象时,系统自动调用拷贝构造函数*/
}
int main()
{
Point A(1,2);
Point B(A); /*当用类的一个对象去初始化该类的另一个对象时系统自动
调用拷贝构造函数实现拷贝赋值*/
fun1(A); /*若函数的形参为类对象,调用函数时,实参赋值给形参,
系统自动调用拷贝构造函数*/
Point c;
c=fun2();
cout<<c.GetX()<<endl;
cout<<B.GetX()<<endl;
}
using namespace std;
class Point{
public:
Point(int xx=0,int yy=0){X=xx;Y=yy;}
Point(const Point &p);
int GetX(){return X;}
int GetY(){return Y;}
private:
int X,Y;
};
Point::Point(const Point& p)
{
X=p.X;
Y=p.Y;
cout<<"拷贝构造函数调用"<<endl;
}
void fun1(Point p)
{
cout<<p.GetX()<<endl;
}
Point fun2()
{
Point A(1,2);
return A; /*当函数的返回值是类对象时,系统自动调用拷贝构造函数*/
}
int main()
{
Point A(1,2);
Point B(A); /*当用类的一个对象去初始化该类的另一个对象时系统自动
调用拷贝构造函数实现拷贝赋值*/
fun1(A); /*若函数的形参为类对象,调用函数时,实参赋值给形参,
系统自动调用拷贝构造函数*/
Point c;
c=fun2();
cout<<c.GetX()<<endl;
cout<<B.GetX()<<endl;
}