【发布时间】:2015-05-28 08:13:17
【问题描述】:
a.*b 运算符在 c++ 中的作用是什么?我找到了这个参考: “对象 a 的成员 b 指向的对象”,但在以下示例中不起作用:
class Color {
public:
int red,green,blue;
Color():red(255), green(255), blue(255){}
Color(int red, int green, int blue) :red(red), green(green), blue(blue){}
void printColor(){
cout << "Red:" << red << " Green:" << green << " Blue:" << blue << endl;
}
};
class Chair{
public:
Color* color;
private:
int legs;
float height;
public:
Chair(int legs, float height):legs(legs), height(height){
color = new Color(255, 0 , 0);
}
void printChair(){
cout << "Legs: " << getLegs() << " , height: " << getHeight() << endl;
}
int getLegs() { return legs; }
float getHeight(){ return height; }
Chair& operator+(Chair& close_chair){
this->legs += close_chair.getLegs();
this->height += close_chair.getHeight();
return *this;
}
};
int main(){
Chair my_chair(4, 1.32f);
my_chair.*color.printColor();
return 0;
}
当我使用 my_chair.*color.printColor();主要,我得到“颜色”:未声明的标识符。我在 Visual Studio 中运行此示例。
谢谢。
【问题讨论】:
-
.* (member access through pointer to member), -
需要显示的部分不是...
-
您是否粘贴了错误的代码?
-
.*正在取消引用 pointer-to-member,而不是作为指针的成员。你在哪里找到那个“参考”?
标签: c++ visual-c++ operators