[英]C++ Override Pure Virtual Function with Function Pointer
If I have a pure virtual function can it be overriden with a function pointer? 如果我有一个纯虚函数,可以使用函数指针覆盖它吗? Scenario below (I'm aware that it's not 100% syntactically correct): 下面的情况(我知道这在语法上不是100%正确的):
#include<iostream>
using namespace std;
class A {
public:
virtual void foo() = 0;
};
class B : public A {
public:
B() { foo = &B::caseOne; }
void caseOne() { cout << "Hello One" << endl; }
void caseTwo() { cout << "Hello Two" << endl; }
void (B::*foo)();
void chooseOne() { foo = &B::caseOne; }
void chooseTwo() { foo = &B::caseTwo; }
};
int main() {
B b;
b.(*foo)();
}
EDIT: In case anyone's interested, here's how I accomplished what I wanted to do: 编辑:如果有人感兴趣,这就是我完成我想做的事情的方式:
#include<iostream>
using namespace std;
class A {
public:
virtual void foo() = 0;
};
class B : public A {
public:
B() { f = &B::caseOne; }
void caseOne() { cout << "Hello One" << endl; }
void caseTwo() { cout << "Hello Two" << endl; }
void (B::*f)();
void chooseOne() { f = &B::caseOne; }
void chooseTwo() { f = &B::caseTwo; }
void foo() { (this->*f)(); }
};
int main() {
B b;
b.foo();
b.chooseTwo();
b.foo();
}
The output is: 输出为:
Hello One
Hello Two
No. And you use this wrong. 否。您使用此错误。 In your code you are trying to assign member-function pointer to function-pointer - it's cannot be compiled. 在您的代码中,您尝试将成员函数指针分配给函数指针-无法对其进行编译。
C++03 standard 10.3/2 C ++ 03标准10.3 / 2
If a virtual member function vf is declared in a class Base and in a class Derived, derived directly or indirectly from Base, a member function vf with the same name and same parameter list as Base::vf is declared, then Derived::vf is also virtual (whether or not it is so declared) and it overrides Base::vf. 如果在Base类中以及从Base直接或间接派生的Derived 类中声明了虚拟成员函数vf,则声明 与Base :: vf具有相同名称和相同参数列表的成员函数vf,然后Derived :: vf还是虚拟的(无论是否声明),它都覆盖Base :: vf。
As @ForEveR said, your code cannot compile. 正如@ForEveR所说,您的代码无法编译。 However, since what you actually need is the ability of switching B
's implementation of foo
in the runtime, we do have workaround: 但是,由于您真正需要的是能够在运行时切换B
的foo
实现的功能,因此我们有解决方法:
#include <iostream>
using namespace std;
class A {
public:
virtual void foo() = 0;
};
class B : public A {
private:
void (B::*_f)();
public:
B() { chooseOne(); }
void caseOne() {
cout << "case one" << endl;
}
void caseTwo() {
cout << "case two" << endl;
}
void chooseOne() { _f = &B::caseOne; }
void chooseTwo() { _f = &B::caseTwo; }
void foo() {
(this->*_f)();
}
};
int main(int argc, const char *argv[])
{
A* b = new B();
b->foo();
((B*)b)->chooseTwo();
b->foo();
return 0;
}
UPDATE : 更新 :
Just found the OP added his answer in the question, which is almost the same as mine. 刚发现OP在问题中添加了他的答案,这与我的答案几乎相同。 But I think calling foo
through pointer instead of instance object is better, for that can exhibit the effect of polymorphism. 但是我认为通过指针而不是实例对象调用foo
更好,因为它可以表现出多态性。 Besides, it's better to hide f
as a private member function. 此外,最好将f
隐藏为私有成员函数。
I think when compile time, the syntax can NOT be compiled. 我认为在编译时,无法编译语法。 You should provide an override function with the certain name and same args list. 您应该提供具有特定名称和相同参数列表的覆盖功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.