在C++中使用指向函数的指针有问题。下面是我的例子:
#include <iostream>
using namespace std;
class bar
{
public:
void (*funcP)();
};
class foo
{
public:
bar myBar;
void hello(){cout << "hello" << endl;};
};
void byebye()
{
cout << "bye" << endl;
}
int main()
{
foo testFoo;
testFoo.myBar.funcP = &byebye; //OK
testFoo.myBar.funcP = &testFoo.hello; //ERROR
return 0;
}
编译器在testFoo.myBar.funcP = &testFoo.hello;
返回错误
ISO C++禁止使用绑定成员函数的地址来形成指向成员函数的指针。说'&foo::hello'
无法在赋值中将“void(foo::)()”转换为“”void()()“”
所以我试着这样做:
class bar
{
public:
void (*foo::funcP)();
};
但是现在编译器又增加了一个:
'foo‘尚未声明为
有没有办法让它工作?
提前感谢您的建议
发布于 2010-03-04 05:19:33
综合每个人的建议,你的最终解决方案将如下所示:
#include <iostream>
using std::cout;
usind std::endl;
class foo; // tell the compiler there's a foo out there.
class bar
{
public:
// If you want to store a pointer to each type of function you'll
// need two different pointers here:
void (*freeFunctionPointer)();
void (foo::*memberFunctionPointer)();
};
class foo
{
public:
bar myBar;
void hello(){ cout << "hello" << endl; }
};
void byebye()
{
cout << "bye" << endl;
}
int main()
{
foo testFoo;
testFoo.myBar.freeFunctionPointer = &byebye;
testFoo.myBar.memberFunctionPointer = &foo::hello;
((testFoo).*(testFoo.myBar.memberFunctionPointer))(); // calls foo::hello()
testFoo.myBar.freeFunctionPointer(); // calls byebye()
return 0;
}
C++ FAQ Lite对如何简化语法提供了一些指导。
将Chris的想法付诸实践,你可以得到这样的东西:
#include <iostream>
using std::cout; using std::endl;
class foo;
typedef void (*FreeFn)();
typedef void (foo::*MemberFn)();
class bar
{
public:
bar() : freeFn(NULL), memberFn(NULL) {}
void operator()(foo* other)
{
if (freeFn != NULL) { freeFn(); }
else if (memberFn != NULL) { ((other)->*(memberFn))(); }
else { cout << "No function attached!" << endl; }
}
void setFreeFn(FreeFn value) { freeFn = value; memberFn = NULL; }
void setMemberFn(MemberFn value) { memberFn = value; freeFn = NULL; }
private:
FreeFn freeFn;
MemberFn memberFn;
};
class foo
{
public:
bar myBar;
void hello() { cout << "foo::hello()" << endl; }
void operator()() { myBar(this); }
};
void bye() { cout << "bye()" << endl; }
int main()
{
foo testFoo;
testFoo();
testFoo.myBar.setMemberFn(&foo::hello);
testFoo();
testFoo.myBar.setFreeFn(&bye);
testFoo();
return 0;
}
发布于 2010-03-04 05:02:28
正如错误所说,方法属于类,而不是单个实例。因此,指向自由函数的指针和指向非静态方法的指针是完全不同的。您还需要一个实例来调用该方法。
//declaring and taking the address of a foo's method
void (foo::*method)() = &foo::hello; //as the compiler nicely suggests
//calling a function through pointer
free_func();
//calling a method through pointer
foo instance;
(instance.*method)();
您可以使用诸如Boost.Bind和Boost.Function (我认为也在std::tr1中)这样的库来抽象这些差异,还可以将一个实例绑定到该方法:
#include <iostream>
#include <boost/bind.hpp>
#include <boost/function.hpp>
using namespace std;
class foo
{
public:
void hello(){cout << "hello" << endl;};
};
void byebye()
{
cout << "bye" << endl;
}
int main()
{
foo testFoo;
boost::function<void()> helloFunc(boost::bind(&foo::hello, testFoo));
boost::function<void()> byeFunc(byebye);
helloFunc();
byeFunc();
return 0;
}
发布于 2010-03-04 04:52:46
要使第二个选项起作用,请声明foo,以便编译器知道它是一个类。
还要注意,您的函数指针语法是不正确的。*
恰好出现在变量名称之前:
class foo;
class bar
{
public:
void (foo::*funcP)();
};
https://stackoverflow.com/questions/2374847
复制相似问题