c ++虚拟类,子类和自引用

got*_*ch4 0 c++ virtual inheritance

考虑这个课程:

class baseController {

    /* Action handler array*/

std::unordered_map<unsigned int, baseController*> actionControllers;

protected:

    /**
     *  Initialization. Can be optionally implemented.
     */
    virtual void init() {

    }

    /**
     * This must be implemented by subclasses in order to implement their action
     * management
     */
    virtual void handleAction(ACTION action, baseController *source) = 0;

    /**
     * Adds an action controller for an action. The actions specified in the
     * action array won't be passed to handleAction. If a controller is already
     * present for a certain action, it will be replaced.
     */
    void attachActionController(unsigned int *actionArr, int len,
            baseController *controller);

    /**
     *
     *  checks if any controller is attached to an action
     *
     */
    bool checkIfActionIsHandled(unsigned int action);

    /**
     *
     *  removes actions from the action-controller filter.
     *  returns false if the action was not in the filter.
     *  Controllers are not destoyed.
     */
    bool removeActionFromHandler(unsigned int action);

public:

    baseController();

    void doAction(ACTION action, baseController *source);

};

}
Run Code Online (Sandbox Code Playgroud)

和这个子类

class testController : public baseController{

    testController tc;

protected:

    void init(){
        cout << "init of test";
    }



    void handleAction(ACTION action, baseController *source){

        cout << "nothing\n";

    }

};
Run Code Online (Sandbox Code Playgroud)

编译器在成员的子类上出现错误

testController tc;
Run Code Online (Sandbox Code Playgroud)

..saying

error: field ‘tc’ has incomplete type
Run Code Online (Sandbox Code Playgroud)

但是,如果我删除它,我实例化它的工作...是否有办法避免这个错误??? 它看起来很奇怪....

jk.*_*jk. 6

one day someone asked me why a class can't contain an instance of itself and i said;
  one day someone asked me why a class can't contain an instance of itself and i said;
    one day someone asked me why a class can't contain an instance of itself and i said;
      ...
Run Code Online (Sandbox Code Playgroud)

使用间接.一个(智能)指针或对testController而不是testController的引用.