我有Presenter
類,它繼承自ModelListener
。正向聲明類的解引用對象
class Model;
class ModelListener
{
public:
ModelListener() : model(0) {}
virtual ~ModelListener() {}
void bind(Model* m) { model = m; }
protected:
Model * model;
};
此外,我有一個實例Presenter presenter
。當我致電presenter->model->PrintName()
時,它當然不會編譯,因爲model
受保護爲presenter
。但是,如果我再進一步改變Model * model
公衆這樣
class Model;
class ModelListener
{
public:
ModelListener() : model(0) {}
virtual ~ModelListener() {}
void bind(Model* m) { model = m; }
//protected:
Model * model;
};
我得到:
MyView.cpp: error: invalid use of incomplete type 'class Model' presenter->model->PrintName(); ModelListener.hpp: error: forward declaration of 'class Model'.
爲什麼我得到這個錯誤會public
而不是當protected
什麼時候?