Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Private virtual function in derived class [duplicate]

Tags:

c++

Possible Duplicate:
C++: overriding public\private inheritance

class base {
public:
  virtual void doSomething() = 0;
};

class derived : public base {
private:   // <-- Note this is private

  virtual void doSomething()
  { cout << "Derived fn" << endl; }
};

Now if I do the following:

base *b = new child;
b->doSomething();    // Calls the derived class function even though that is private

Question:

  1. It's able to call the derived class function even though it is private. How is this possible?

Now if I change the inheritance access specifier from public to protected/private, I get a compilation error:

'type cast' : conversion from 'Derived *' to 'base *' exists, but is inaccessible

Note: I am aware of the concepts of the inheritance access specifiers. So in the second case as it's derived private/protected, it's inaccessible. But I wonder about the answer to first question. Any input will be highly appreciated.

like image 777
user1706047 Avatar asked Nov 29 '12 04:11

user1706047


People also ask

Can virtual function be overridden in its derived class?

The virtual keyword can be used when declaring overriding functions in a derived class, but it is unnecessary; overrides of virtual functions are always virtual. Virtual functions in a base class must be defined unless they are declared using the pure-specifier.

Can a virtual function be declared as protected or private in the derived classes?

A virtual function can be private as C++ has access control, but not visibility control. As mentioned virtual functions can be overridden by the derived class but under all circumstances will only be called within the base class.

Does virtual function have different functionality in derived class?

A virtual function is a member function of a base class that is overridden by a derived class. When you use a pointer or a reference to the base class to refer to a derived class object, you can call a virtual function for that object and have it run the derived class's version of the function.

Can private be accessed by derived class?

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.


2 Answers

Access control is implemented at compile time, not run-time, while polymorphism (including the use of virtual functions) is a run-time feature.

like image 74
Healer Avatar answered Oct 02 '22 13:10

Healer


In the first case the access check is done (as it is always done) on the static type that the call is made through. The static type of *b is base, and in that case doSomething() is public.

C++03 11.6 "Access to virtual functions" says:

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]

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 (B* in the example above). The access of the member function in the class in which it was defined (D in the example above) is in general not known.

Keep in mind especially that "the access of the member function in the class in which it was defined (D in the example above) is in general not known". In general, at the point in your example where b->doSomething(); is called, the compiler may have no knowledge at all about derived (or child), much less whether or not the access to derived::doSomething() is private.

like image 39
Michael Burr Avatar answered Oct 02 '22 12:10

Michael Burr