开发者

virtual function in private or protected inheritance

It's easy to understand the virtual function in public inheritance. So what's the point for virtual function in private or protected inheritance?

For开发者_运维技巧 example:

class Base {
public:
virtual void f() { cout<<"Base::f()"<<endl;}
};

class Derived: private Base {
public: 

void f() { cout<<"Derived::f()"<<endl;}

};

Is this still called overriding? What's the use of this case? What's the relationship of these two f()?

Thanks!


Private inheritance is just an implementation technique, not an is-a relationship, as Scott Meyers explains in Effective C++:

class Timer {
public:
    explicit Timer(int tickFrequency);
    virtual void onTick() const; // automatically called for each tick
    ...
};

class Widget: private Timer {
private:
    virtual void onTick() const; // look at Widget private data
    ...
};

Widget clients shouldn't be able to call onTick on a Widget, because that's not part of the conceptual Widget interface.


Your f() method is still overridden. This relationship is useful when implementing the Template Method design pattern. Basically, you'd implement common sets of operations in the base class. Those base class operations would then invoke a virtual method, like your f(). If the derived class overrides f(), the base class operations end up calling the derived version of f(). This allows derived classes to keep the base algorithm the same but alter the behavior to suit their needs. Here's a trivial example:

#include <iostream>

using namespace std;

class Base
{
public:
  virtual void f() { cout<<"Base::f()" << endl; }
protected:
  void base_foo() { f(); }
};

class DerivedOne: private Base
{
public: 
  void f() { cout << "Derived::f()" << endl;}
  void foo() { base_foo(); }
};

class DerivedTwo: private Base
{
public: 
  void foo() { base_foo(); }
};

int main()
{
  DerivedOne d1;
  d1.foo();

  DerivedTwo d2;
  d2.foo();
}

Here's the result at run-time:

$ ./a.out 
Derived::f()
Base::f()

Both derived classes call the same base class operation but the behavior is different for each derived class.


  • There doesn't need to be a point to every combination of different features. You're simply allowed to combine them.
  • A virtual protected member is accessible to derived classes, so it's useful to them.
  • A virtual private member is accessible to friend classes, so it's useful to them.


Both private and protected inheritance allow overriding virtual functions in the private/protected base class and neither claims the derived is a kind-of its base.

Protected inheritance allows derived classes of derived classes to know about the inheritance relationship and still override the virtual functions.

Privately inheriting from the Base class in your Derived class, destroys all conceptual ties between the derived and base class. The derived class is just implemented in terms of the base class, nothing more. Private inheritance is just an implementation technique and implies no relationship between the classes involved.


An example would be:

/// Thread body interface
class runnable
{
public:

    virtual ~runnable() {}
    virtual void run() =0;
};

/// Starts OS thread, calls p->run() in new thread
thread_id start_thread( runnable* p );

/// Has a private thread
class actor: private runnable, private noncopyable
{
private:

    thread_id tid; /// private thread

public:

    actor() { tid = start_thread( this ); } // here this IS-A runnable
    // ...
    virtual ~actor() { stop_thread( tid ); }

private:

    virtual void run() { /* work */ }
};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜