考虑这个类:C++虚拟类,子类和selfreference
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;
..saying
error: field ‘tc’ has incomplete type
但是,如果我删除,我instatiate类它的工作......有没有办法避免这个错误?它看起来对我来说太奇怪了......
你在你熟悉Java的另外一个问题提到的,但新的C++。一个重要的区别是类变量声明的含义,比如'Class object;'。在C++中,这个变量是'Class'的一个实际实例,而不是Java中的引用。 – 2009-11-12 14:18:24