考慮這個類: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