有以下类定义    classPoint{    public:       Point(int x=0,int y=0){_x=x;_y=y;}       void Move (int xOff,int yOff       {_x +=xOff;_y

admin2017-10-17  5

问题 有以下类定义    classPoint{    public:       Point(int x=0,int y=0){_x=x;_y=y;}       void Move (int xOff,int yOff       {_x +=xOff;_y+yOff}       void Print() const       {cout<<’(’<<_x<<’,’<<_y<<’)’<<endl;}    private:       int_x_y;    };    下列语句中会发生编译错误的是

选项 A、Pointpt;pt;Print();
B、const Point pt;pt.Print();
C、Pointpt;pt.Move(1,2);
D、const Point pt;pt.Move(1,2);

答案D

解析 本题考核常对象、常数据成员与常成员函数。如果将一个对象说明为常对象,则通过该常对象只能调用它的常成员函数,不能调用其他的成员函数,D选项中对象 pt为常对象,而成员函数Move()不是常成员函数,所以这样调用会发生编译错误。
转载请注明原文地址:https://kaotiyun.com/show/DPAp777K
0

最新回复(0)