首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

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

2012-03-07 
关于派生类访问基类protected对象C/C++ code基类Queryclass Query {public:const vectorlocation* locat

关于派生类访问基类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 只能被派生类访问。
那个不是他的派生类。

热点排行