开发者

c++ virtual class, subclass and selfreference

开发者_StackOverflowconsider this class:

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);

};

}

and this subclass

class testController : public baseController{

    testController tc;

protected:

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



    void handleAction(ACTION action, baseController *source){

        cout << "nothing\n";

    }

};

The compiler comes out with an error on the subclass on the member

testController tc;

..saying

error: field ‘tc’ has incomplete type

but if I remove that and I instatiate the class it works... is there a way to avoid this error??? It looks so strange to me....


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;
      ...

use indirection. a (smart) pointer or refrence to a testController rather than a testController.


Your code is trying to embed an entire instance of testController inside itself, which is impossible. Instead, you want a reference:

testController &tc;

or a pointer

testController *tc;


It won't compile because you're declaring a member variable 'tc' that is an instance of itself. You're not using tc in the subclass; what is your intent here?


You can not create an object of the class inside that class itself. Probably what you intend to do is to keep a pointer to the class. In that case you should use it as testController* BTW, why do you want to do that? It looks a bit strange to me.


(A bit late to the party, but ...)

Maybe gotch4 meant to type something like this?

class testController : public baseController
{
public:
    testController tc();  // <- () makes this a c'tor, not a member variable

    // ( ... snip ... )
};
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜