[英]Virtual method causes compilation error in Derived class
Consider the next code : 考虑下一个代码:
#include <iostream>
using namespace std;
class A
{
public:
virtual int f() {cout <<"A:: f()\n"; return 1;}
virtual void f(int) {cout <<"A:: f(int)\n";}
virtual void g() {cout <<"A::g()\n";}
};
class B3 : public A
{
public:
void f() {cout <<"B3::f ()\n";}
};
int main()
{
return 0;
}
It produces the following error : 它产生以下错误:
..\main.cpp:17: error: conflicting return type specified for 'virtual void B3::f()'
..\main.cpp:9: error: overriding 'virtual int A::f()'
but why ? 但为什么 ? in the worst case I'd think I'd have an Hiding case , but instead I get compilation error regarding A's
virtual int f() {cout <<"A:: f()\\n"; return 1;}
在最坏的情况下,我会认为我有一个Hiding的情况,但是相反,我得到了有关A的
virtual int f() {cout <<"A:: f()\\n"; return 1;}
编译错误virtual int f() {cout <<"A:: f()\\n"; return 1;}
virtual int f() {cout <<"A:: f()\\n"; return 1;}
thanks ,Ronen 谢谢,罗宁
Don't confuse overriding with hiding. 不要将覆盖与隐藏混淆。 You override virtuals.
您覆盖虚拟。
Your class definition is equivalent to: 您的类定义等效于:
class B3 : public A
{
public:
virtual void f() {cout <<"B3::f ()\n";}
};
Once a function declared virtual, it will remain virtual for all classes deriving from your base class, regardless of whether you explicitly declare it virtual or not. 一旦将函数声明为虚拟函数,则无论您是否显式声明为虚拟函数,该函数对于从基类派生的所有类都将保持虚拟状态。 Therefore you are trying to override a virtual function and changing its return type, which is illegal in C++.
因此,您试图覆盖虚拟函数并更改其返回类型,这在C ++中是非法的。 If the function were not virtual, you would simply be hiding the base class implementation, therefore changing the return type is valid.
如果该函数不是虚函数,则只需隐藏基类实现,因此更改返回类型是有效的。 There would be no ambiguity since the compiler would know where to call the function from and what return type to expect.
毫无疑问,因为编译器会知道从何处调用该函数以及期望返回什么类型。
However, consider having: 但是,请考虑具有:
A* a;
....
a->f();
What would af() return? af()返回什么? a is a pointer to A, but can point to an object of type B3.
a是指向A的指针,但可以指向B3类型的对象。 So it either returns an int or doesn't return anything.
所以它要么返回一个int要么不返回任何东西。 See the ambiguity here?
在这里看到歧义吗?
Instead, no polymorphism involved, 相反,不涉及多态性,
A a;
a.f();
will cal f from a, same as b3.f would call f from B3. 将从b计算f,与从b3调用b相同。f从B3调用f。 All in all, overriding base class functions implies keeping the same return type.
总而言之,覆盖基类函数意味着保持相同的返回类型。 If you want to create a new function with a different return type, change its signature (either its name or its parameters - or both).
如果要创建具有其他返回类型的新函数,请更改其签名(其名称或参数-或两者)。
Anyway, you shouldn't even be doing this... Why would you want to have a function with the same name and no parameters return different things? 无论如何,您甚至都不应该这样做……为什么要拥有一个具有相同名称且没有参数返回不同东西的函数? Wouldn't adding a separate function be more readable?
添加一个单独的功能是否更易读?
You'd have hiding if f()
would have had a different parameter list, or not declared as virtual on the base class. 如果
f()
具有不同的参数列表,或者在基类上未声明为virtual,那么您将隐藏起来。 In the former case, since overloading doesn't cross inheritance borders, A
's f
would have been hidden. 在前一种情况下,由于重载不会跨越继承边界,因此
A
的f
将被隐藏。 But this is not the case, since you have f()
on both classes, which only differ in return value. 但是事实并非如此,因为在两个类上都有
f()
,它们的返回值只是不同。 Return values covariance is the only difference allowed, and since it's not the case (void does not inherit from int), you get the error. 返回值协方差是唯一允许的差异,并且既然不是这种情况(void不会从int继承),则会出现错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.