jeudi 22 janvier 2015

private function member called outside of class

Why B::f() is called even though it is private? i know this fact that : Access is checked at the call point using the type of the expression used to denote the object for which the member function is called.



#include <iostream>

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

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

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

int main() {
B b;
C(b);
}

Aucun commentaire:

Enregistrer un commentaire