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 }
D::f()
is private, D
is public inherited from B
, so the public function f
in B
D
(I know without inheritance, member access is private by default)f
is a virtual function in B
, so if we call b.f()
, 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?
In multiple inheritance,where all the base class contains same function name with different functionality, we can access the protected function from particular base class using "::" scope resolution operator.
To access a member function by pointer, we have to declare a pointer to the object and initialize it (by creating the memory at runtime, yes! We can use new keyboard for this). The second step, use arrow operator -> to access the member function using the pointer to the object.
3. Which member can never be accessed by inherited classes? Explanation: The private member functions can never be accessed in the derived classes. The access specifiers is of maximum security that allows only the members of self class to access the private member functions.
Private members of the base class cannot be used by the derived class unless friend declarations within the base class explicitly grant access to them. In the following example, class D is derived publicly from class B . Class B is declared a public base class by this declaration.
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 b.f(), 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 b.f()
, 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With