->* 运算符到底是什么?

发布于 2024-12-08 17:44:45 字数 259 浏览 0 评论 0原文

我以前从未使用过它,只是在一篇文章中偶然发现它......我认为它相当于 *x->y 但显然事实并非如此。

这是我尝试过的,但给了我一个错误:

struct cake {
 int * yogurt;
} * pie;

int main(void) {
 pie = new cake;
 pie->yogurt = new int;
 return pie->*yogurt = 4;
}

I've never used it before and just stumbled upon it in an article... I thought it would be the equivalent to *x->y but apparently it isn't.

Here's what I tried, and gave me an error:

struct cake {
 int * yogurt;
} * pie;

int main(void) {
 pie = new cake;
 pie->yogurt = new int;
 return pie->*yogurt = 4;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

堇色安年 2024-12-15 17:44:45

当您有指向成员函数的指针时使用它。

当你有一个指向类的函数的指针时,你调用它的方式与调用任何成员函数

object.membername( ... )

objectptr->membername( ... )

几乎相同,但是当你有一个成员函数指针,后面需要加一个*。或->以便编译器理解接下来是一个变量,而不是要调用的函数的实际名称。

这是一个如何使用它的示例。

class Duck
{
public:

  void quack() { cout << "quack" << endl; }
  void waddle() { cout << "waddle" << endl; }
};

typedef void (Duck::*ActionPointer)();

ActionPointer myaction = &Duck::quack;

void takeDuckAction()
{    
    Duck myduck;
    Duck *myduckptr = &myduck;

    (myduck.*myaction)();
    (myduckptr->*myaction)();
}

Its used when you have pointers to member functions.

When you have a pointer to a function of a class, you call it in much the same way you would call any member function

object.membername( ... )

or

objectptr->membername( ... )

but when you have a member function pointer, an extra * is needed after the . or -> in order that the compiler understand that what comes next is a variable, not the actual name of the function to call.

Here's an example of how its used.

class Duck
{
public:

  void quack() { cout << "quack" << endl; }
  void waddle() { cout << "waddle" << endl; }
};

typedef void (Duck::*ActionPointer)();

ActionPointer myaction = &Duck::quack;

void takeDuckAction()
{    
    Duck myduck;
    Duck *myduckptr = &myduck;

    (myduck.*myaction)();
    (myduckptr->*myaction)();
}
七七 2024-12-15 17:44:45

它定义了一个指向成员的指针

在包含 –>* 运算符的表达式中,第一个操作数必须
是指定类型的“指向类类型的指针”类型
第二个操作数,或者它必须是明确派生自的类型
那个班。
MSDN

It defines a pointer to a member.

In an expression containing the –>* operator, the first operand must
be of the type "pointer to the class type" of the type specified in
the second operand, or it must be of a type unambiguously derived from
that class.
MSDN

旧时模样 2024-12-15 17:44:45

.* 和 ->* 运算符将指向类或结构的成员函数。如果更改行,下面的代码将显示如何使用 .* 运算符的简单示例:
Value funcPtr = &Foo::One;Value funcPtr = &Foo::Two; 显示的结果将更改为 1000,因为该函数是 inValue *2

例如取自此处

#include <iostream>
#include <stdlib.h>

class Foo { 
  public: 
    double One( long inVal ) { return inVal*1; }
    double Two( long inVal ) { return inVal*2; }
}; 

typedef double (Foo::*Value)(long inVal); 

int main( int argc, char **argv ) { 
  Value funcPtr = &Foo::One; 

  Foo foo;

  double result = (foo.*funcPtr)(500); 
  std::cout << result << std::endl;
  system("pause");
  return 0; 
} 

The .* and ->* operators will point to member functions of a class or structure. The code below will show a simple example of how to use the .* operator, if you change the line:
Value funcPtr = &Foo::One; to Value funcPtr = &Foo::Two; the result displayed will change to 1000 since that function is inValue*2

for example Taken From Here:

#include <iostream>
#include <stdlib.h>

class Foo { 
  public: 
    double One( long inVal ) { return inVal*1; }
    double Two( long inVal ) { return inVal*2; }
}; 

typedef double (Foo::*Value)(long inVal); 

int main( int argc, char **argv ) { 
  Value funcPtr = &Foo::One; 

  Foo foo;

  double result = (foo.*funcPtr)(500); 
  std::cout << result << std::endl;
  system("pause");
  return 0; 
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文