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);
}
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
Login in to like
Login in to comment