C++ 从超类调用子类方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15743110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ Call subclass method from superclass
提问by ChrisGeo
I have code of the following style:
我有以下样式的代码:
class SubClass;
class SuperClass;
class SuperClass {
private:
void bar() {
SubClass().foo();
}
};
class SubClass : SuperClass {
public:
void foo() {};
};
So basically I have a SuperClass from where I want to call a method foo() of the subclass. VS 2012 gives me the following errors:
所以基本上我有一个 SuperClass,我想从中调用子类的方法 foo()。VS 2012 给了我以下错误:
Error 1 error C2514: 'SubClass' : class has no constructors.
错误 1 错误 C2514:“子类”:类没有构造函数。
Error 2 error C2228: left of '.foo' must have class/struct/union.
错误 2 错误 C2228:'.foo' 的左边必须有类/结构/联合。
What is the correct structure for what I want to do?
我想做的事情的正确结构是什么?
回答by Kiril Kirov
You can't do this. You must (at least) declare the method in the base class. For example:
你不能这样做。您必须(至少)在基类中声明该方法。例如:
#include <iostream>
class SuperClass
{
public:
void bar()
{
foo();
}
private:
virtual void foo() // could be pure virtual, if you like
{
std::cout << "SuperClass::foo()" << std::endl;
}
};
class SubClass : public SuperClass // do not forget to inherit public
{
public:
virtual void foo() { std::cout << "SubClass::foo()" << std::endl; }
};
int main()
{
SuperClass* pTest = new SubClass;
pTest -> bar();
delete pTest;
}
will print SubClass::foo()
.
将打印SubClass::foo()
。
回答by riv
First, you have to realise that calling SubClass().foo()
has nothing to do with the current object - it will create a new SubClass
object and call its foo
member. If you want the bar
function to call foo
on the current object, you need to declare foo
as a virtual function in the base class, as Kiril suggested.
首先,您必须意识到调用SubClass().foo()
与当前对象无关 - 它会创建一个新SubClass
对象并调用其foo
成员。如果您希望bar
函数foo
在当前对象上调用,则需要foo
在基类中声明为虚函数,如 Kiril 建议的那样。
However, if you do want to call foo
on a new object (which doesn't make much sense, but whatever), you are doing it correctly. Your example didn't compile because SubClass
is forward declared, but not defined before bar
function - you might want to move the implementation of bar
below the definition of SubClass
, like so:
class SubClass;
class SuperClass;
但是,如果您确实想调用foo
一个新对象(这没有多大意义,但无论如何),您就做对了。您的示例未编译,因为SubClass
是前向声明的,但未在bar
函数之前定义 - 您可能希望将 的实现移动bar
到 的定义下方,如下所示SubClass
:班级超班;
class SuperClass {
private:
void bar();
};
class SubClass : SuperClass {
public:
void foo() {};
};
void SuperClass::bar() {
SubClass().foo();
}
回答by asdru
you can try with some CRTP, ie:
您可以尝试使用一些 CRTP,即:
#include <iostream>
class SubClass;
template<typename T>
class SuperClass {
public:
void bar() {
((T *) this)->foo();
}
};
class SubClass : public SuperClass<SubClass> {
public:
void foo() {
std::cout << "hello crtp!" << std::endl;
};
};
int main(void){
SubClass a = SubClass();
a.bar();
return 0;
}