【问题标题】:c++ virtual class, subclass and selfreferencec++虚类、子类和自引用
【发布时间】:2009-11-12 14:01:52
【问题描述】:

考虑这个类:

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

};

}

还有这个子类

class testController : public baseController{

    testController tc;

protected:

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



    void handleAction(ACTION action, baseController *source){

        cout << "nothing\n";

    }

};

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

testController tc;

..说

error: field ‘tc’ has incomplete type

但是如果我删除它并启动它工作的类......有没有办法避免这个错误???这对我来说看起来很奇怪......

【问题讨论】:

    标签: c++ inheritance virtual


    【解决方案1】:
    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;
          ...
    

    使用间接。指向 testController 而不是 testController 的(智能)指针或引用。

    【讨论】:

      【解决方案2】:

      您的代码试图将testController 的整个实例嵌入到自身内部,这是不可能的。相反,您需要参考:

      testController &tc;
      

      或指针

      testController *tc;
      

      【讨论】:

      • 您在另一个问题中提到您熟悉Java,但对C++ 很陌生。一个重要的区别是类变量声明的含义,例如Class object;。在 C++ 中,此变量是 Class 的实际实例,而不是 Java 中的引用。
      【解决方案3】:

      它不会编译,因为你声明了一个成员变量'tc',它是它自己的一个实例。您没有在子类中使用 tc ;你在这里的意图是什么?

      【讨论】:

        【解决方案4】:

        您不能在该类本身内创建该类的对象。可能您打算做的是保留指向该类的指针。在这种情况下,您应该将其用作testController* BTW,您为什么要这样做?我觉得有点奇怪。

        【讨论】:

        • 我只是在测试基类......它的目的是容纳许多相同类型的控制器
        【解决方案5】:

        (聚会有点晚了,但是……)

        也许 gotch4 打算输入这样的内容?

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

        【讨论】:

        • 谢谢...但五年后我真的不记得为什么我确实遇到了这个问题!
        • 当然,但是其他人将来可能会偶然发现这个问题,并提示“也许您打算添加构造函数而不是成员变量声明?”可能会帮助他们。
        猜你喜欢
        • 2020-07-16
        • 1970-01-01
        • 2010-09-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-07
        • 1970-01-01
        相关资源
        最近更新 更多