#include <iostream>
#include<cstdio>
#include<typeinfo>
using std::cout;
using std::endl;
class foo;
class bar
{
public:
bar()
{
}
bar(void (*getNextValue)(void)):memberFunctionPointer(getNextValue)
{
}
void calltoderivedslass()
{
// *memberFunctionPointer();
((bar*)this)->bar::memberFunctionPointer;
}
void (foo::*memberFunctionPointer)();
};
class foo : public bar
{
public:
foo():bar(static_cast<foo::*memberFunctionPointer>(&foo::hello))
{
}
void hello()
{
printf("Hello \n\n");
}
};
void byebye()
{
cout << "bye" << endl;
}
int main()
{
foo testfoo;
//((testfoo).*(testfoo.memberFunctionPointer))();
return 0;
}
classname.cpp: In constructor "bar::bar(void (*)())":
classname.cpp:15:68: error: cannot convert "void (*)()" to "void (foo::*)()" in initialization
classname.cpp: In constructor "foo::foo()":
classname.cpp:29:25: error: expected type-specifier
classname.cpp:29:25: error: expected ">"
classname.cpp:29:25: error: expected "("
classname.cpp:29:30: error: expected unqualified-id before "*" token
classname.cpp:31:2: error: expected "{" at end of input
期望:
我要初始化的基类函数指针初始化它指向派生类成员函数。我想在创建派生类的对象时进行初始化。从基类我想使用获得的函数指针调用派生类函数。
在此先感谢所有人。
如果使用OOP正确的,没有必要使用函数指针.. 。考虑一下程序员谁需要维护你的代码.. –
是啊,有要求使用函数指针。 当数据库读取发生某种错误时,必须调用一个回调函数,所以我需要函数指针,而不是难以维护代码。谢谢:) –