虚方法导致派生类中的编译错误 [英] Virtual method causes compilation error in Derived class

查看:26
本文介绍了虚方法导致派生类中的编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑下一个代码:

#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;
}

它产生以下错误:

..\main.cpp:17: error: conflicting return type specified for 'virtual void B3::f()'
..\main.cpp:9: error:   overriding 'virtual int A::f()'

但是为什么?在最坏的情况下,我认为我会有一个隐藏案例,但是我收到了关于 A 的 virtual int f() {cout <<"A:: f()\n"; 的编译错误.返回 1;}

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;}

谢谢,罗宁

推荐答案

不要将覆盖与隐藏混淆.您覆盖虚拟.

Don't confuse overriding with hiding. You override virtuals.

你的类定义相当于:

class B3 : public A
{
public:
    virtual void f() {cout <<"B3::f ()\n";}
};

一旦函数声明为虚函数,它对于派生自基类的所有类都将保持虚函数,无论您是否显式声明它为虚函数.因此,您试图覆盖虚函数并更改其返回类型,这在 C++ 中是非法的.如果函数不是虚拟的,您将只是隐藏基类实现,因此更改返回类型是有效的.不会有歧义,因为编译器会知道从哪里调用函数以及期望的返回类型.

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++. 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.

但是,请考虑:

A* a;
....
a->f();

a-f() 会返回什么?a 是指向 A 的指针,但可以指向类型为 B3 的对象.所以它要么返回一个 int 要么不返回任何东西.看到这里的歧义了吗?

What would a-f() return? a is a pointer to A, but can point to an object of type B3. So it either returns an int or doesn't return anything. See the ambiguity here?

相反,不涉及多态性,

A a;
a.f();

将从 a 中调用 f,与 b3.f 从 B3 中调用 f 相同.总而言之,覆盖基类函数意味着保持相同的返回类型.如果您想创建一个具有不同返回类型的新函数,请更改其签名(名称或参数 - 或两者).

will cal f from a, same as b3.f would call f from B3. 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?

这篇关于虚方法导致派生类中的编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆