如何从其他 class 调用函数指针
How to Call function pointer from other class
假设我有简单的 class 和一些简单的函数指针,比如:
class ClassWithFuncPointer
{
public:
inline void firstFunction() { /* do something */ };
inline void secondFunction() { /* do something */ };
// MY FUNCTION POINTER
void (ClassWithFuncPointer::*funcPointer) ();
// AND I CAN DEFINE IT BY DEFAULT IN CONSTRUCTOR LIKE THAT:
ClassWithFuncPointer()
{
funcPointer = &ClassWithFuncPointer::firstFunction;
}
// AND NOW I CAN USE MY FUNCTION POINTER INSIDE OF ClassWithFuncPointer, LIKE THAT:
void useFunctionPointer()
{
(this->*funcPointer )();
}
}
所以这里 (this->*funcPointer )();
完成任务。
但我不知道如何使用我的 funcPointer
来自其他 class,我的意思是这样的:
class otherClass
{
otherClass(){};
ClassWithFuncPointer instanceOfClassWithFuncPointer;
}
现在如何在 instanceOfClassWithFuncPointer
的成员 otherClass
中使用 funcPointer
。有可能吗?
我尝试了很多变体:
(this->*instanceOfClassWithFuncPointer.funcPointer)();
或
(instanceOfClassWithFuncPointer.*funcPointer)();
或
( (&instanceOfClassWithFuncPointer)->*funcPointer )();
或者只是
instanceOfClassWithFuncPointer.funcPointer();
但总是报错。想不通了。
如下(仅限 C++11 或更新版本)怎么样?
auto fp = instanceOfClassWithFuncPointer.funcPointer;
(instanceOfClassWithFuncPointer.*fp)();
或者(C++98 兼容,可能使用更短的变量名)?
(instanceOfClassWithFuncPointer.*instanceOfClassWithFuncPointer.funcPointer)();
以下是完整的工作示例
#include <iostream>
struct ClassWithFuncPointer
{
public:
inline void firstFunction ()
{ std::cout << "cwfp::firstFunction()" << std::endl; }
inline void secondFunction ()
{ std::cout << "cwfp::secondFunction()" << std::endl; }
void (ClassWithFuncPointer::*funcPointer) ();
ClassWithFuncPointer()
{ funcPointer = &ClassWithFuncPointer::firstFunction; }
void useFunctionPointer()
{ (this->*funcPointer )(); }
};
class otherClass
{
public:
otherClass ()
{ }
ClassWithFuncPointer instanceOfClassWithFuncPointer;
void foo ()
{
auto fp = instanceOfClassWithFuncPointer.funcPointer;
(instanceOfClassWithFuncPointer.*fp)();
}
};
int main ()
{
otherClass oc;
oc.foo();
}
假设我有简单的 class 和一些简单的函数指针,比如:
class ClassWithFuncPointer
{
public:
inline void firstFunction() { /* do something */ };
inline void secondFunction() { /* do something */ };
// MY FUNCTION POINTER
void (ClassWithFuncPointer::*funcPointer) ();
// AND I CAN DEFINE IT BY DEFAULT IN CONSTRUCTOR LIKE THAT:
ClassWithFuncPointer()
{
funcPointer = &ClassWithFuncPointer::firstFunction;
}
// AND NOW I CAN USE MY FUNCTION POINTER INSIDE OF ClassWithFuncPointer, LIKE THAT:
void useFunctionPointer()
{
(this->*funcPointer )();
}
}
所以这里 (this->*funcPointer )();
完成任务。
但我不知道如何使用我的 funcPointer
来自其他 class,我的意思是这样的:
class otherClass
{
otherClass(){};
ClassWithFuncPointer instanceOfClassWithFuncPointer;
}
现在如何在 instanceOfClassWithFuncPointer
的成员 otherClass
中使用 funcPointer
。有可能吗?
我尝试了很多变体:
(this->*instanceOfClassWithFuncPointer.funcPointer)();
或
(instanceOfClassWithFuncPointer.*funcPointer)();
或
( (&instanceOfClassWithFuncPointer)->*funcPointer )();
或者只是
instanceOfClassWithFuncPointer.funcPointer();
但总是报错。想不通了。
如下(仅限 C++11 或更新版本)怎么样?
auto fp = instanceOfClassWithFuncPointer.funcPointer;
(instanceOfClassWithFuncPointer.*fp)();
或者(C++98 兼容,可能使用更短的变量名)?
(instanceOfClassWithFuncPointer.*instanceOfClassWithFuncPointer.funcPointer)();
以下是完整的工作示例
#include <iostream>
struct ClassWithFuncPointer
{
public:
inline void firstFunction ()
{ std::cout << "cwfp::firstFunction()" << std::endl; }
inline void secondFunction ()
{ std::cout << "cwfp::secondFunction()" << std::endl; }
void (ClassWithFuncPointer::*funcPointer) ();
ClassWithFuncPointer()
{ funcPointer = &ClassWithFuncPointer::firstFunction; }
void useFunctionPointer()
{ (this->*funcPointer )(); }
};
class otherClass
{
public:
otherClass ()
{ }
ClassWithFuncPointer instanceOfClassWithFuncPointer;
void foo ()
{
auto fp = instanceOfClassWithFuncPointer.funcPointer;
(instanceOfClassWithFuncPointer.*fp)();
}
};
int main ()
{
otherClass oc;
oc.foo();
}