初始化时将函数作为构造函数参数传递给 base class 构造函数
passing function as constructor parameter to base class constructor while initializing
#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
期望:
我想初始化基class函数指针来初始化它指向派生的class成员函数。我想在创建派生 class 的对象时初始化它。从基础 class 我想使用获取的函数指针调用派生的 class 函数。
在此先感谢大家。
在我看来像是虚拟方法:
class bar
{
public:
bar()
{
}
void calltoderivedslass()
{
this->hello();
}
virtual void hello() = 0;
};
class foo : public bar
{
public:
foo( )
{
}
void hello() override
{
printf("Hello \n\n");
}
};
另一种方法可能是使用 Curiously Recurring Template Pattern (CRTP) 来实现静态多态性:
template<typename T>
class bar
{
public:
bar()
{
}
void calltoderivedslass()
{
static_cast<T*>(this)->hello();
}
};
class foo : public bar<foo>
{
public:
foo( )
{
}
void hello()
{
printf("Hello \n\n");
}
};
如果你真的想保留指向成员函数的指针,你可以考虑 std::function
绑定到 this
:
class bar
{
public:
bar()
{
}
template<typename F>
bar(F&& getNextValue):memberFunctionPointer(std::forward<F>(getNextValue))
{
}
void calltoderivedslass()
{
this->memberFunctionPointer();
}
std::function<void()> memberFunctionPointer;
};
class foo : public bar
{
public:
foo( ):bar(std::bind(&foo::hello, this))
{
}
void hello()
{
printf("Hello \n\n");
}
};
随着使用时间的延长,我猜:
void byebye()
{
cout << "bye" << endl;
}
int main()
{
bar testbar(byebye);
testbar.calltoderivedslass(); // not a derived class, but it works
return 0;
}
您 bar
的构造函数是错误的。
构造函数 bar(void (*getNextValue)(void))
不期望 foo
函数的成员函数指针,因此无法使用 getNextValue
类型初始化 bar::memberFunctionPointer
。
您需要将构造函数中的参数更改为 void (foo::*getNextValue)(void)
才能编译。
然而整体设计对我来说似乎并不正确......所以我认为@wasthishelpful 的回答更有帮助;-)
#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
期望:
我想初始化基class函数指针来初始化它指向派生的class成员函数。我想在创建派生 class 的对象时初始化它。从基础 class 我想使用获取的函数指针调用派生的 class 函数。
在此先感谢大家。
在我看来像是虚拟方法:
class bar
{
public:
bar()
{
}
void calltoderivedslass()
{
this->hello();
}
virtual void hello() = 0;
};
class foo : public bar
{
public:
foo( )
{
}
void hello() override
{
printf("Hello \n\n");
}
};
另一种方法可能是使用 Curiously Recurring Template Pattern (CRTP) 来实现静态多态性:
template<typename T>
class bar
{
public:
bar()
{
}
void calltoderivedslass()
{
static_cast<T*>(this)->hello();
}
};
class foo : public bar<foo>
{
public:
foo( )
{
}
void hello()
{
printf("Hello \n\n");
}
};
如果你真的想保留指向成员函数的指针,你可以考虑 std::function
绑定到 this
:
class bar
{
public:
bar()
{
}
template<typename F>
bar(F&& getNextValue):memberFunctionPointer(std::forward<F>(getNextValue))
{
}
void calltoderivedslass()
{
this->memberFunctionPointer();
}
std::function<void()> memberFunctionPointer;
};
class foo : public bar
{
public:
foo( ):bar(std::bind(&foo::hello, this))
{
}
void hello()
{
printf("Hello \n\n");
}
};
随着使用时间的延长,我猜:
void byebye()
{
cout << "bye" << endl;
}
int main()
{
bar testbar(byebye);
testbar.calltoderivedslass(); // not a derived class, but it works
return 0;
}
您 bar
的构造函数是错误的。
构造函数 bar(void (*getNextValue)(void))
不期望 foo
函数的成员函数指针,因此无法使用 getNextValue
类型初始化 bar::memberFunctionPointer
。
您需要将构造函数中的参数更改为 void (foo::*getNextValue)(void)
才能编译。
然而整体设计对我来说似乎并不正确......所以我认为@wasthishelpful 的回答更有帮助;-)