#include<iostream>
using namespace std;
class Point {
public:
Point(int a, int b);
Point(const Point& p);
void print()
{
cout << x << " " << y << endl;
}
private:
int x, y;
};
Point::Point(int a, int b)
{
x = a;
y = b;
cout << "Using normal constructor." << endl;
}
Point::Point(const Point& p)
{
x = 2 * p.x;
y = 2 * p.y;
cout << "Using copy constructor." << endl;
}
void fun1(Point p)
{
p.print();
}
Point fun2()
{
Point p4(10, 30);
return p4;
}
int main()
{
Point p1(30, 40);
p1.print();
Point p2(p1);
p2.print();
Point p3 = p1;
p3.print();
fun1(p1);
p2 = fun2();
p2.print();
return 0;
}
书上的例子我完全照抄,结果却不一样,这个最后一行结果是10 30,应该为20 60。
书上说对象作返回值时会调用拷贝函数赋值给一个临时对象,那应该会×2倍啊。而且应该会输出Using copy constructor.但实际情况并没有,我想知道这是为什么!
感谢回答!