新手问题:有关友元函数的
#include <iostream>
#include <cmath>
using namespace std;
class Point
{
public:
Point(int xx=0,int yy=0){X=xx;Y=yy;}
int GetX(){return X;}
int GetY(){return Y;;}
friend float fDist(Point &a,Point &b);
private:
int X,Y;
};
float fDist(Point &p1,Point &p2)
{
double x=double(p1.X-p2.X);
double y=double(p1.Y-p2.Y);
return sqrt(x*x+y*y);
}
int main()
{
void fun();
Point myp1(0,1),myp2(4,5);
cout < < "The distance is: ";
cout < <fDist(myp1,myp2) < <endl;
}
源程序如上。
问:对于“friend float fDist(Point &a,Point &b);”,能否改为:“friend float fDist(Point a,Point b);”,这样做会对程序产生什么影响?
[解决办法]
可以, 但是调那个友员函数时会需额外调用构造函数, 所以不efficient.