What will be printed out as a result of the following code execution?
#include <iostream>

class A {
    public:
        virtual void f() { std::cout << "A"; }
};

class B : public A {
    private:
        void f() { std::cout << "B"; }
};

void g(A& a) { a.f(); }

int main () {
    B b;
    g(b);
}
Explanation
B::f() will be called, even though it is in a private access area. According to the standard, access is checked at the function call point, based on the type of variable through which the function is called. Here, the call point is the place a.f(); and the type of variable is A& a

Следи за CodeGalaxy

Мобильное приложение Beta

Get it on Google Play
Обратная Связь
Cosmo
Зарегистрируйся сейчас
или Подпишись на будущие тесты