为什么保护继承时,基类成员只能由直接派生类访问,而无法继承下去

THEALVA 2015-04-12 11:19:56
在程序员面试宝典以及网上多个帖子上看到,私有继承和保护继承方式下,基类的成员只能由直接派生类访问,不能再往下继承。对于私有继承,我认为这是正确的。可是,对于保护继承,应该是基类的共有成员和保护成员在直接派生类中都变成了保护成员,但是保护成员不是还可以被直接派生类的子类访问吗?那就应该还可以继承下去呀,不解……
...全文
407 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
我自西向 2015-12-17
  • 打赏
  • 举报
回复
我也疑惑宝典这句话了,我自己验证了下,发现结果和你一样,protected继承使基类的protected和public在派生类变为protected,同时还是可以被派生类的子类中的函数使用的,也就是说它还是可以往下继承的。不知你最终是否得出相同的结论。
THEALVA 2015-04-15
  • 打赏
  • 举报
回复
引用 3 楼 fly_dragon_fly 的回复:
If a class is declared to be a base class for another class using the protected access specifier, the public and protected members of the base class are accessible as protected members of the derived class. 你看的书不会有翻译错误吧
您引用的这句话,应该是说保护继承,父类的public和protected对于派生类来说就都是protected了。不过我想问的是这个派生类应该还能再派生下去否?
THEALVA 2015-04-15
  • 打赏
  • 举报
回复
引用 1 楼 zhangxiangDavaid 的回复:
你的想法是对的

class A
{
protected:
	int a;
};
class B:protected A
{

};
class C :protected B
{
public:
	void fun()
	{
		a = 520;
		cout << "a=" << a << endl;
	}
};
int main(void)
{
	C c;
	c.fun();

	return 0;
}
//c=520
谢谢
苏叔叔 2015-04-15
  • 打赏
  • 举报
回复
引用 7 楼 yinqiaohua 的回复:
[quote=引用 2 楼 zhangxiangDavaid 的回复:] 哦,不对,是a=520
您引用的这句话,应该是说保护继承,父类的public和protected对于派生类来说就都是protected了。不过我想问的是这个派生类应该还能再派生下去否? [/quote] 当然可以
THEALVA 2015-04-15
  • 打赏
  • 举报
回复
引用 2 楼 zhangxiangDavaid 的回复:
哦,不对,是a=520
您引用的这句话,应该是说保护继承,父类的public和protected对于派生类来说就都是protected了。不过我想问的是这个派生类应该还能再派生下去否?
THEALVA 2015-04-15
  • 打赏
  • 举报
回复
引用 3 楼 fly_dragon_fly 的回复:
If a class is declared to be a base class for another class using the protected access specifier, the public and protected members of the base class are accessible as protected members of the derived class. 你看的书不会有翻译错误吧
谢谢指点
THEALVA 2015-04-15
  • 打赏
  • 举报
回复
引用 楼主 yinqiaohua 的回复:
在程序员面试宝典以及网上多个帖子上看到,私有继承和保护继承方式下,基类的成员只能由直接派生类访问,不能再往下继承。对于私有继承,我认为这是正确的。可是,对于保护继承,应该是基类的共有成员和保护成员在直接派生类中都变成了保护成员,但是保护成员不是还可以被直接派生类的子类访问吗?那就应该还可以继承下去呀,不解……
多谢
fly_dragon_fly 2015-04-15
  • 打赏
  • 举报
回复
引用 10 楼 yinqiaohua 的回复:
[quote=引用 3 楼 fly_dragon_fly 的回复:] If a class is declared to be a base class for another class using the protected access specifier, the public and protected members of the base class are accessible as protected members of the derived class. 你看的书不会有翻译错误吧
您引用的这句话,应该是说保护继承,父类的public和protected对于派生类来说就都是protected了。不过我想问的是这个派生类应该还能再派生下去否?[/quote]如果标准没有改,目前是可以的
fly_dragon_fly 2015-04-13
  • 打赏
  • 举报
回复
If a class is declared to be a base class for another class using the protected access specifier, the public and protected members of the base class are accessible as protected members of the derived class. 你看的书不会有翻译错误吧
ztenv 版主 2015-04-13
  • 打赏
  • 举报
回复
你的书可能会有问题,再者,对于有疑问的,找几个编译器试试不就知道了?
苏叔叔 2015-04-12
  • 打赏
  • 举报
回复
哦,不对,是a=520
苏叔叔 2015-04-12
  • 打赏
  • 举报
回复
你的想法是对的

class A
{
protected:
	int a;
};
class B:protected A
{

};
class C :protected B
{
public:
	void fun()
	{
		a = 520;
		cout << "a=" << a << endl;
	}
};
int main(void)
{
	C c;
	c.fun();

	return 0;
}
//c=520

64,649

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧