C++ Parent class calling a child virtual function

前端 未结 7 1047
半阙折子戏
半阙折子戏 2021-02-13 22:34

I want a pure virtual parent class to call a child implementation of a function like so:

class parent
{
  public:
    void Read() { //read stuff }
    virtual vo         


        
7条回答
  •  半阙折子戏
    2021-02-13 23:03

    You need to wrap in inside an object that calls the virtual method after the object is fully constructed:

    class parent
    {
      public:
        void Read() { /*read stuff*/ }
        virtual void Process() = 0;
        parent()
        {
            Read();
        }
    };
    
    class child: public parent
    {
      public:
        virtual void Process() { /*process stuff*/ }
        child() : parent() { }
    };
    
    template
    class Processor
    {
        public:
            Processor()
                :processorObj() // Pass on any args here
            {
                processorObj.Process();
            }
        private:
            T   processorObj;
    
    };
    
    
    
    
    int main()
    {
       Processor c;
    }
    

提交回复
热议问题