.* 运算符在 C++ 中做什么?
What does .* operator do in C++?
a.*b 运算符在 C++ 中有什么作用?我找到了这个参考:
"Object pointed to by member b of object a",但在以下示例中不起作用:
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();主要是,我得到 'color' :未声明的标识符。我运行这个例子在Visual Studio。
谢谢。
.*
是解引用一个指向成员的指针,但你只是想解引用一个成员指针。为此,请使用 ->
:
my_chair.color->printColor();
(*(my_chair.color)).printColor(); //same thing
在您的示例中使用 .*
类似于:
auto colorP = &Chair::color;
(my_chair.*colorP)->printColor();
如果要取消引用 color
成员,请执行以下操作:
my_chair.color->printColor();
或
(*my_chair.color).printColor();
运算符 .*
取消引用指向成员的指针。
指向成员的指针 - "member pointer" - 不同于作为指针的成员。
它不指向 "into" class 的特定实例,因此您需要一个 "pointer" 可以关联的实例。
示例:
struct A
{
int x;
int y;
};
int main()
{
A a{1, 78};
// Get a pointer to the x member of an A
int A::* int_member = &A::x;
// Prints a.x
std::cout << a.*int_member << std::endl;
// Point to the y member instead
int_member = &A::y;
// Prints a.y
std::cout << a.*int_member << std::endl;
}
a.*b 运算符在 C++ 中有什么作用?我找到了这个参考: "Object pointed to by member b of object a",但在以下示例中不起作用:
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();主要是,我得到 'color' :未声明的标识符。我运行这个例子在Visual Studio。
谢谢。
.*
是解引用一个指向成员的指针,但你只是想解引用一个成员指针。为此,请使用 ->
:
my_chair.color->printColor();
(*(my_chair.color)).printColor(); //same thing
在您的示例中使用 .*
类似于:
auto colorP = &Chair::color;
(my_chair.*colorP)->printColor();
如果要取消引用 color
成员,请执行以下操作:
my_chair.color->printColor();
或
(*my_chair.color).printColor();
运算符 .*
取消引用指向成员的指针。
指向成员的指针 - "member pointer" - 不同于作为指针的成员。
它不指向 "into" class 的特定实例,因此您需要一个 "pointer" 可以关联的实例。
示例:
struct A
{
int x;
int y;
};
int main()
{
A a{1, 78};
// Get a pointer to the x member of an A
int A::* int_member = &A::x;
// Prints a.x
std::cout << a.*int_member << std::endl;
// Point to the y member instead
int_member = &A::y;
// Prints a.y
std::cout << a.*int_member << std::endl;
}