友元类函数的定义是 friend double dist(Point &p1,Point &p2);
但是 把&去掉 friend double dist(Point p1,Point p2);
不会提示错误 反而调用了复制构造函数是为什么?
#include
#include
using namespace std;
class Point
{
public:
Point (int x=0,int y=0):x(x),y(y)
{
cout<<"构造函数被调用"<<endl;
}
Point (Point &p)
{
cout<<"复制构造函数被调用"<<endl;
x=p.x;
y=p.y;
}
friend double dist(Point &p1,Point &p2);
private:
int x,y;
};
double dist(Point &p1,Point &p2)
{
double x=p2.x-p1.x;
double y=p2.y-p1.y;
return sqrt(x*x+y*y);
}
int main()
{
Point mp1(1,1),mp2(4,5);
cout<<"len=";
cout<<dist(mp1,mp2)<<endl;
return 0;
}