What will the next program code print on the console?
#include <iostream>

class Base
    virtual void Foo() { std::cout << "Base::Foo "; }
    virtual ~Base() {}

class Derived: public Base
    int Foo() { std::cout << "Derived::Foo "; return int();}

int main()
    Base *d = new Derived;
    delete d;
    return 0;
The code will not be compiled because the Derived::Foo() function differs from Base::Foo() only by the type of the returned value. That's why the usual overload of functions won't work, as overloaded functions cannot differ only by the type of returned value. Also, the overload of the virtual function will not work, because the exact matching of the function signatures is necessary for this purpose. There is an exception to this rule when the overloaded virtual function returns a pointer or a reference to the class object in which it is defined. See return type relaxation and covariant return type.

Слідкуй за CodeGalaxy

Мобільний додаток Beta

Get it on Google Play
Зворотній Зв’язок
Зареєструйся Зараз
або Підпишись на майбутні тести