z1226834370 发表于 2015-11-1 12:00:34

c++ 友元类参数问题

#include<iostream>
#include<cmath>
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="<<dist(mp1,mp2)<<endl;
        return 0;
}

为什么把dist的参数前面的地址符去掉结果变了
求大神指点
#include<iostream>
#include<cmath>
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="<<dist(mp1,mp2)<<endl;
        return 0;
}

为什么把dist的参数前面的地址符去掉结果变了
求大神指点
页: [1]
查看完整版本: c++ 友元类参数问题