读书人

关于派生类访问基类protected对象解决

发布时间: 2012-03-07 09:13:51 作者: rapoo

关于派生类访问基类protected对象

C/C++ code
基类Queryclass Query {public:const vector<location>* locations() const { return &_loc; }// ...protected:vector<location> _loc;// ...};派生类:NameQueryboolNameQuery::compare( const Query *pquery ){// ok: 自己的 Query 子对象的 protected 成员int myMatches = _loc.size();// 错误: 没有 "直接访问另一个独立的 Query// 对象的 protected 成员" 的权利int itsMatches = pquery->_loc.size();return myMatches == itsMatches;}

请问为什么“int itsMatches = pquery->_loc.size();”这是错误的?




[解决办法]
楼主理解有误吧
protected成员不能被类的对象访问
[解决办法]
protected 只能被派生类访问。
那个不是他的派生类。

读书人网 >C++

热点推荐