简体   繁体   中英

Access of member functions in C++ inheritance

I am just confused about the tiny program on inheritance below:

#include<iostream>
using namespace std;

struct B {
    virtual int f() { return 1; }
}; // f is public in B

class D : public B { 
    int f() { return 2; }
}; // f is private in D

int main()
{
    D d;
    B& b = d;
    cout<<b.f()<<endl; // OK: B::f() is public, D::f() is invoked even though it's private
    cout<<d.f()<<endl; // error: D::f() is private
}
  1. I can't figure out why D::f() is private, D is public inherited from B , so the public function f in B
    is also public in D (I know without inheritance, member access is private by default)
  2. f is a virtual function in B , so if we call bf() , we actually call D::f() , but just as the illustration mentioned, why D::f() is able to be invoked even though it's private?

Can anyone explain the simple inheritance problem in detail?

This has to do that with virtual dispatch is a runtime concept. The class B doesn't care which class extends it, and it doesn't care if it's private or public because it can't know.

I can't figure out why D::f() is private, D is public inherited from B, so the public function f in B is also public in D(I know without inheritance, member access is private by default)

D::f() is private because you made it private. This rule isn't affect by inheritance nor virtual dispatch.

f is a virtual function in B, so if we call bf(), we actually call D::f(), but just as the illustration mentioned, why D::f() is able to be invoked even though it's private?

Because in reality, when invoking bf() , the compiler has no idea which function will actually be called. It will simply call the function f() , and since B::f is virtual, the called function will be chosen at runtime . The runtime program has no information about which function is private or protected. It only know functions.

If the function is chosen at runtime, the compiler can't know at compile-time what function will be called, and the access specifier can't be known. In fact, the compiler won't even try to check if the function called will be private or not. The access specifier could be in some code that the compiler haven't seen yet.

As you've experienced, you can't call D::f directly. This is exactly what private will do: prohibit direct access of the member. You can, however, access it indirectly through a pointer or a reference. The virtual dispatch you use will do that internally.

Access specifiers apply to the function name only, they aren't some restriction on how or when the function can be called by other means. A private function could be called outside the class if it is made available by some means other than its name (for example, a function pointer).

For a class declared with class keyword, the default access-specifier is private . Your code is the same as:

// ...
class D: public B
{
private:
  int f() { return 2; }
};

As you can see, f is private in D . It makes no difference what the access specifier was of any function in B with the same name. Be clear in your mind that B::f() and D::f() are two different functions.

The effect of the virtual keyword is that if f() without a scope qualifier is called on a B reference that refers to a D object, then even though it resolves to B::f() , actually D::f() is invoked instead.

This process still uses the access specifier for B::f() : access is checked at compile-time; but it might be run-time matter as to which function is called.

The C++ Standard has an exact example of this:

11.5 Access to virtual functions [class.access.virt]

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 ]

Can't explain it any clearer.

The answer given illustrates what is being done, but why would you ever want to do this, where the base class calls private virtual functions?

Well, there is a design pattern called the template method pattern that uses this technique of having a base class that calls private virtual functions in the derived class.

struct B 
{
    virtual ~B() {};
    int do_some_algorithm()
    {
       do_step_1();
       do_step_2();
       do_step_3();
    }

    private:
          virtual void do_step_1() {}
          virtual void do_step_2() {}
          virtual void do_step_3() {}
};

class D : public B 
{ 
   void do_step_1() 
   {
      // custom implementation
   }
   void do_step_2() 
   {
      // custom implementation
   }

   void do_step_3() 
   {
      // custom implementation
   }
};

int main()
{
   D dInstance;
   B * pB = &dInstance;
   pB->do_some_algorithm();
}

This allows us to not expose the custom steps of class D to the public interface, but at the same time allows B to call these functions using a public function.

This actually has less to do with virtual dispatch and more to do with what access specifiers mean.

The function itself is not private ; its name is.

Consequently, the function cannot be named outside of the scope of the class, eg from main . However, you can still do so through a name that is public (ie the base's virtual function that is overridden) or from a scope in which the function's name is accessible despite the private qualifier (eg a member function of that class).

That's just how it works.

why D::f() is able to be invoked even though it's private?

To understand virtual function mechanism it is good to know, how it is usually implemented. A function at runtime is actually no more than an address in memory where the executable code of the function body locates. To call the function we need to know its address (a pointer). C++ object with virtual functions representation in memory contains so called vtable - an array of pointers to the virtual functions.

vtable典型的实现

Key point is that in derived classes vtable repeats (and may extend) the vtable of base class, but if the virtual function is overriden its pointer is replaced in derived object's vtable.

When a virtual function call is done through the base class pointer, the address of the virtual function is calculated as an offset in the vtable array. No other checks are done, just the function address is taken. If it is a base class object, it will be the address of the base class function. If it is a derived class object, it will be the address of the derived class function, does not matter if it was declared private or not.

This how it works.

Member of struct is default to be public, and member of class is default to be private. So f() in B is public and when it's derived to D, because you didn't explicitly declare it is public, so according to rules of derivation, it became private.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM