Private function member called outside of class

test program

In the example below, why is B::f() 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);
}
Lightness Races in Orbit

Because the standard says so:

[C++11: 11.5/1]: The access rules (Clause 11) for a virtual function are determined by its declaration and are not affected by the rules for a function that later overrides it. [ Example:

class B {
public:
  virtual int f();
};
class D : public B {
private:
  int f();
};
void f() {
  D d;
  B* pb = &d;
  D* pd = &d;
  pb->f();       // OK: B::f() is public,
                 // D::f() is invoked
  pd->f();       // error: D::f() is private
}

—end example ]

The example is the same as yours, lol.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why aren't we allowed to access a private static member outside class without public member function?

From Dev

Passing a member function of a class to a parameter outside the class

From Dev

Calling private function in lambda from outside the class

From Dev

How to define a member function outside class in python?

From Dev

C++ - Change private member from outside the class

From Dev

Use private variable of a class in its member function

From Dev

Having a class member function call a function outside the class

From Dev

How do i get a class member function to have access to another class member function's private member?

From Dev

Class-function that returns a struct which is a private member of that class

From Dev

Why can't a PRIVATE member function be a friend function of another class?

From Dev

java private class member

From Dev

Object as private member of a class

From Dev

Can I define a private template function outside of a class?

From Dev

C++ - Define member function outside template-class but in header

From Dev

How to get a general function pointer as a private member of a C++ class?

From Dev

Using friend function, can we overwrite the private member of the class?

From Dev

How to access private static variable in static member function of another class?

From Dev

How to get a general function pointer as a private member of a C++ class?

From Dev

How to set the Return Type of a Class Member Function as the object of a Private Struct

From Dev

tkinter: label not displayed when pack() is called outside of a class' init function

From Dev

Use typedef member from a template class outside of the class as return type for member function

From Dev

Why constructor is not being called in member function of another class in C++?

From Dev

Does the member function of class template not get instantiated if never be called?

From Dev

Does the member function of class template not get instantiated if never be called?

From Dev

Why can't this public member function call decltype on a private struct member declared inside the class?

From Dev

Can a private static member be used as a default argument to a member function of its class?

From Dev

Accessing C++ class public member function from private struct data member

From Dev

Private static member in base class

From Dev

Return private static member of a class

Related Related

  1. 1

    Why aren't we allowed to access a private static member outside class without public member function?

  2. 2

    Passing a member function of a class to a parameter outside the class

  3. 3

    Calling private function in lambda from outside the class

  4. 4

    How to define a member function outside class in python?

  5. 5

    C++ - Change private member from outside the class

  6. 6

    Use private variable of a class in its member function

  7. 7

    Having a class member function call a function outside the class

  8. 8

    How do i get a class member function to have access to another class member function's private member?

  9. 9

    Class-function that returns a struct which is a private member of that class

  10. 10

    Why can't a PRIVATE member function be a friend function of another class?

  11. 11

    java private class member

  12. 12

    Object as private member of a class

  13. 13

    Can I define a private template function outside of a class?

  14. 14

    C++ - Define member function outside template-class but in header

  15. 15

    How to get a general function pointer as a private member of a C++ class?

  16. 16

    Using friend function, can we overwrite the private member of the class?

  17. 17

    How to access private static variable in static member function of another class?

  18. 18

    How to get a general function pointer as a private member of a C++ class?

  19. 19

    How to set the Return Type of a Class Member Function as the object of a Private Struct

  20. 20

    tkinter: label not displayed when pack() is called outside of a class' init function

  21. 21

    Use typedef member from a template class outside of the class as return type for member function

  22. 22

    Why constructor is not being called in member function of another class in C++?

  23. 23

    Does the member function of class template not get instantiated if never be called?

  24. 24

    Does the member function of class template not get instantiated if never be called?

  25. 25

    Why can't this public member function call decltype on a private struct member declared inside the class?

  26. 26

    Can a private static member be used as a default argument to a member function of its class?

  27. 27

    Accessing C++ class public member function from private struct data member

  28. 28

    Private static member in base class

  29. 29

    Return private static member of a class

HotTag

Archive