virtual function in private or protected inheritance

前端 未结 5 2211
予麋鹿
予麋鹿 2021-02-12 22:52

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:

5条回答
  •  别那么骄傲
    2021-02-12 23:07

    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 */ }
    };
    

提交回复
热议问题