注册 登录
编程论坛 C++教室

有一点不明白

花栖醉露 发布于 2018-12-03 12:50, 1403 次点击
#include <iostream>
using namespace std;
class Point{  
public:
    Point(int i,int j)    {
        x=i;
        y=j;
        cout<<"Point Constructor\n";
    }
    Point(Point &tempP){
        x=tempP.x+3;
        y=tempP.y+3;
        cout<<"Point Copy_Constructor\n";
    }
    int getX(){        return x;    }
    int getY(){        return y;    }
~Point(){cout<<"Point  Destructor\n";}
void print(){
    cout<<"pointX="<<x<<",pointY="<<y<<endl;
}
private:
    int x,y;   
};
int main(){   
    Point a1(2,3);
    a1.print();
    Point a2(a1);
    a2.print();
    Point *pa=new Point(8,9);
    pa->print();
    cout<<pa->getY()-pa->getX()<<endl;
    delete pa;
    return 0;
}


运行结果中的  1 是怎么来的!!求教!
运行出来是:
Point Constructor
pointX=2,pointY=3
Point Copy_Constructor
pointX=5,pointY=6
Point Constructor
pointX=8,pointY=9
1
Point  Destructor
Point  Destructor
Point  Destructor
请按任意键继续. . .

2 回复
#2
rjsp2018-12-03 13:35
cout<<pa->getY()-pa->getX()<<endl;
9-8 就是 1

BTW:你这代码……
#3
花栖醉露2018-12-03 19:33
回复 2楼 rjsp
我的天!怎么会没注意到这行!
1