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

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

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

int main()
{
    Base *d = new Derived;
    d->Foo();
    delete d;
    return 0;
}
Explanation
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
Обратная Связь
Cosmo
Зарегистрируйся сейчас
или Подпишись на будущие тесты