且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

虚函数的返回类型不同

更新时间:2023-11-11 18:04:52

由于胡说八道:

struct foo
{
    virtual int get() const { return 0; }
};

struct bar : foo
{
    std::string get() const { return "this certainly isn't an int"; }
};

int main()
{
    bar b;
    foo* f = &b;

    int result = f->get(); // int, right? ...right?
}

让派生类返回完全不相关的内容是不明智的.

It isn't sensible to have a derived class return something completely unrelated.