简体   繁体   English

虚方法导致派生类中的编译错误

[英]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. 在前一种情况下,由于重载不会跨越继承边界,因此Af将被隐藏。 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.

相关问题 调用派生类的虚方法会导致段错误 - Calling virtual method of a derived class causes segfaults 模板类方法实例化当基类中的虚拟无关方法导致MSVC上的编译失败时 - template class method instantiation when a virtual unrelated method in the base class causes compilation failure on MSVC 在派生类中声明为Virtual的函数无法执行,而是出现编译错误,为什么? - Function declared as Virtual in derived class doesn't execute, instead gave compilation error, Why? 在派生类中声明虚拟方法是错误的,该派生类的基类没有虚拟方法? - Is it an error to declare a virtual method in a derived class whose base does not have a virtual method? 通过派生类虚方法调用基类虚方法 - Calling base class virtual method by derived class virtual method 派生类中虚拟方法的默认实现 - Default implementation for virtual method in derived class 在派生类中实现虚拟方法时遇到问题 - Problem implementing virtual method in derived class 在每个派生的 class 中重新定义一个虚方法 - redefine a virtual method in each derived class 虚拟函数无法解析大多数派生类方法 - virtual function is not resolving to most derived class method 纯虚拟基础方法在派生类中的专业化 - Specialization of pure virtual base method in derived class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM