[英]typedef + pointer to a member function
I get an error when initializing a typedef-ed pointer to a member function. 初始化成员函数的类型定义的指针时出现错误。 Is it because it is typedef-ed before classs declaration?
是因为它是在类声明之前进行类型定义的? If so, how can I forward declare the typedef so it can be used inside of struct Definition which is then used inside of the B class itself.
如果是这样,我如何转发声明typedef,以便可以在struct Definition中使用它,然后在B类本身内部使用。
Header file: 头文件:
typedef void (B::* p)()
struct Definition {
p code = nullptr;
void execute() {
code();
}
}
class B
{
private:
void semicolon();
p o;
void (B::* pointer)();
std::list<Definition> definitions;
void definePrimitives();
}
Source file: 源文件:
void B::definePrimitives() {
o= B::semicolon;// error: expression must be a modifiable l-value
pointer =&B::semicolon;//works
}
This works: 这有效:
class B;
typedef void (B::* p)();
struct Definition {
void execute() {
p();
}
};
class B
{
private:
void semicolon();
p o;
void (B::* pointer)();
std::list<Definition> definitions;
void definePrimitives() {
o= &B::semicolon;
pointer =&B::semicolon;
}
};
Basically you have to take the pointer to the function, not the function itself 基本上,您必须使用指向函数的指针,而不是函数本身
The code had a couple of problems: 1) it lacked forward declaration of class B as pointed out by IlBeldus. 该代码有两个问题:1)它缺少IlBeldus指出的B类的前向声明。 If you remove forward declaration of class B you will be able to reproduce the error.
如果删除B类的前向声明,则可以重现该错误。 2) the execute method inside of Definition struct was wrong.
2)Definition结构中的execute方法错误。 It should be:
它应该是:
struct Definition {
Code code = nullptr;
void execute(B b) {
b.o = &B::semicolon;
(b.*(b.c))();
}
};
in my real application I've defined pointer to B to as a member of Definition. 在我的实际应用程序中,我已经定义了指向B的指针作为Definition的成员。 One can't just execute member function by
一个人不能仅仅通过执行成员函数
p(); or code();
one needs to specify the object of type B first, like in my example above: 首先需要指定类型B的对象,如上面的示例所示:
(b.*(b.c))();
3) Thirdly, my code required struct Definition to be declared below B, and struct Definition had to be forward-declared before B. just like this: 3)第三,我的代码要求将struct Definition声明在 B 之下 ,而struct Definition必须在B之前进行前声明。
header file: 头文件:
#include <list>
class B;
using Code = void(B::*)(void);
struct Definition;
class B
{
private:
void (B::* pointer)();
std::list<Definition> definitions;
void definePrimitives();
public:
void semicolon();
Code o = nullptr;
Code c = nullptr;
};
struct Definition {
Code code = nullptr;
void execute(B b) {
b.o = &B::semicolon;
(b.*(b.c))();
}
};
and source file: 和源文件:
#include "Header.h"
#include <list>
void B::semicolon()
{
}
void B::definePrimitives() {
o = &B::semicolon;
pointer = &B::semicolon;
}
int main() {
B b;
return 0;
}
4) and.. in the C++ world one my want to use classes instead of structs all the way, and since we are past c++11 already one might want to use the using clause, instead of typedef. 4)和..在C ++世界中,我想一直使用类而不是结构,并且由于我们过去使用c ++ 11,已经有人可能要使用using子句而不是typedef。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.