c++成员函数指针问题

发布于 2024-11-14 01:47:12 字数 790 浏览 4 评论 0原文

我是 c++ 新手。我想了解对象指针和成员函数指针。我编写了如下代码:

代码:

#include <iostream>
using namespace std;
class golu
{
   int i;
public:
   void man()
   {
      cout<<"\ntry to learn \n";
   }
};
int main()
{
   golu m, *n;
   void golu:: *t =&golu::man(); //making pointer to member function

   n=&m;//confused is it object pointer
   n->*t();
}

但是当我编译它时,它显示了两个错误:

pcc.cpp: In function ‘int main()’:
pcc.cpp:15: error: cannot declare pointer to ‘void’ member
pcc.cpp:15: error: cannot call member function ‘void golu::man()’ without object
pcc.cpp:18: error: ‘t’ cannot be used as a function.

我的问题如下:

  1. 我在这段代码中做错了什么?
  2. 如何制作对象指针?
  3. 如何创建指向类成员函数的指针以及如何使用它们?

请解释一下这些概念。

I'm new to c++ . I want to know about object pointer and pointer to member function . I wrote a code which is following:

code :

#include <iostream>
using namespace std;
class golu
{
   int i;
public:
   void man()
   {
      cout<<"\ntry to learn \n";
   }
};
int main()
{
   golu m, *n;
   void golu:: *t =&golu::man(); //making pointer to member function

   n=&m;//confused is it object pointer
   n->*t();
}

but when i compile it it shows me two error which is following:

pcc.cpp: In function ‘int main()’:
pcc.cpp:15: error: cannot declare pointer to ‘void’ member
pcc.cpp:15: error: cannot call member function ‘void golu::man()’ without object
pcc.cpp:18: error: ‘t’ cannot be used as a function.

my question are following :

  1. What I'm doing wrong in this code ?
  2. How to make object pointer ?
  3. How to make pointer to member function of a class and how to use them ?

Please explain me these concept.

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

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

发布评论

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

评论(4

定格我的天空 2024-11-21 01:47:12

这里纠正了两个错误:

int main()
{
   golu m, *n;
   void (golu::*t)() =&golu::man; 

   n=&m;
   (n->*t)();
}
  1. 你想要一个指向函数的指针,运算
  2. 符的优先级不是你所期望的,我不得不添加括号。 n->*t(); 被解释为 (n->*(t())) 而您想要 (n->* t)();

Two errors corrected here:

int main()
{
   golu m, *n;
   void (golu::*t)() =&golu::man; 

   n=&m;
   (n->*t)();
}
  1. you want a pointer to function
  2. the priority of the operators is not the one you expected, I had to add parenthesis. n->*t(); is interpreted as (n->*(t())) while you want (n->*t)();
风吹雨成花 2024-11-21 01:47:12

成员函数指针具有以下形式:

R (C::*Name)(Args...)

其中 R 是返回类型,C 是类类型,Args... 是任何可能的类型函数的参数(或无参数)。

有了这些知识,您的指针应该如下所示:

void (golu::*t)() = &golu::man;

注意成员函数后面缺少的 () 。这将尝试调用您刚刚获得的成员函数指针,如果没有对象,这是不可能的。
现在,使用简单的 typedef 就变得更具可读性:

typedef void (golu::*golu_memfun)();
golu_memfun t = &golu::man;

最后,您不需要指向对象的指针来使用成员函数,但需要括号:

golu m;
typedef void (golu::*golu_memfun)();
golu_memfun t = &golu::man;
(m.*t)();

括号很重要,因为 () 运算符(函数调用)比 具有更高的优先级(也称为 优先级)。 * (和->*)运算符。

A member function pointer has the following form:

R (C::*Name)(Args...)

Where R is the return type, C is the class type and Args... are any possible parameters to the function (or none).

With that knowledge, your pointer should look like this:

void (golu::*t)() = &golu::man;

Note the missing () after the member function. That would try to call the member function pointer you just got and thats not possible without an object.
Now, that gets much more readable with a simple typedef:

typedef void (golu::*golu_memfun)();
golu_memfun t = &golu::man;

Finally, you don't need a pointer to an object to use member functions, but you need parenthesis:

golu m;
typedef void (golu::*golu_memfun)();
golu_memfun t = &golu::man;
(m.*t)();

The parenthesis are important because the () operator (function call) has a higher priority (also called precedence) than the .* (and ->*) operator.

兮子 2024-11-21 01:47:12

'void golu:: *t =&golu::man();'应更改为 'void (golu:: *t)() =&golu::man;'您正在尝试使用指向函数的指针而不是指向静态函数结果的指针!

'void golu:: *t =&golu::man();' should be changed to 'void (golu:: *t)() =&golu::man;' you are trying to use pointer to function not pointer to result of a static function!

jJeQQOZ5 2024-11-21 01:47:12

(1) 函数指针未正确声明。

(2) 您应该这样声明:

void (golu::*t) () = &golu::man;

(3) 成员函数指针应该与的对象一起使用。

(1) Function pointer is not declared properly.

(2) You should declare like this:

void (golu::*t) () = &golu::man;

(3) Member function pointer should be used with object of the class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文