I saw many debates on the web about the access control modifier. And I also have some confusion although I have never used other derive method than "public". And I believe only the compiler will tell you the truth:
见到网上有很多关于C++访问控制符的问题,自己也有些迷糊。虽然自己在实际工程中从来没有用过除public以外的继承。再多的讨论都不如让编译器来告诉你事实来的实在:
// NOTICE: the lines commented out below can get compiled!
class B {
public:
void public_foo() {
cout << "public_foo" << endl;
}
protected:
void protected_foo() {
cout << "protected_foo" << endl;
}
private:
void private_foo() {
cout << "private_foo" << endl;
}
};
class D1 : public B {
public:
void test () {
B::public_foo();
B::protected_foo();
// B::private_foo();
}
};
class D2 : protected B {
public:
void test () {
B::public_foo();
B::protected_foo();
// B::private_foo();
}
};
class D3 : private B {
public:
void test () {
B::public_foo();
B::protected_foo();
// B::private_foo();
}
};
class DD1 : public D1 {
public:
void test_inherits () {
B::public_foo();
B::protected_foo();
// B::private_foo();
}
};
class DD2 : public D2 {
public:
void test_inherits () {
B::public_foo();
B::protected_foo();
// B::private_foo();
}
};
class DD3 : public D3 {
public:
void test_inherits () {
// B::public_foo();
// B::protected_foo();
// B::private_foo();
}
};
int main(int argc, const char *argv[])
{
DD1 dd1;
dd1.public_foo();
// you can't change the originally protected to public
// with public inheritance
// dd1.protected_foo();
// nor the private ones
// dd1.private_foo();
DD2 dd2;
// but you can change a public to protected
// dd2.public_foo();
DD3 dd3;
// DD3 has everthing tagged with private
// you have nothing to do with it
return 0;
}
So:
- the public/protected/private modifier for field or method declaration controls the member's access in its derived class.
- the public/protected/private modifier for inheriting modifies the access defined by the above rule to the modifier you used here.
- the modification made by the above rule only happens if the it makes the member less accessible
结论:
- public/protected/private被用在字段或方法的声明时控制的是该成员在其子类中的访问权
- public/protected/private被用于继承关系时可以修改上述声明的访问权为你在此指定的修饰符
- 上述修改只在当这种修改使得成员访问权“更小”时才会发生
分享到:
相关推荐
Welcome to Turbo C++ Version 3.0 -------------------------------- This README file contains important information about Turbo C++. For the latest information about Turbo C++ and its accompanying ...
modifiers是方法修饰符的集合,也是制作更多的方法。 显然,方法修饰符会修改方法。 具体来说,在 Ruby 术语中,它们是类方法,它们: 取一个符号参数,它命名同一个类的实例方法,和返回相同的符号,但导致对命名...
Java Class and Method Modifiers
该程序包包含闪烁所需的类型(临时)。 为了使用此功能,您必须添加 import '@glint/environment-ember-loose...import { DidUpdateModifier } from '@gavant/glint-template-types/types/ember-render-modifiers/did-
For developers with experience in any language, Victor Shtern’s Core C++ teaches C++ the ...modifiers, dynamic memory management, class composition, inheritance, polymorphism, I/O, and much more.
The C++ Standard Library A Tutorial and Reference (2nd Edition)+cppstdlib-code.zip C++标准库(第二版)英文版.pdf 非扫描版+源代码 Prefaceto the SecondEdition xxiii Acknowledgments for the Second...
avi bmesh freestyle modifiers windowmanager blenfont collada gpu nodes blenkernel compositor ikplugin physics blenlib datatoc imbuf python blenloader depsgraph makesdna quicktime gameengine/ ...
Welcome to Turbo C++ Version 3.0 -------------------------------- This README file contains important information about Turbo C++. For the latest information about Turbo C++ and its accompanying ...
mergeBlockData ( currentEditorState , { foo : 1 } )Moduler导入 import adjustBlockDepth from 'draft-js-modifiers/adjustBlockDepth'// Support Tree Shaking for webpack, rollup.jsimport { insertText } ...
Hagrid是一个用于响应式网站和Web应用程序的混合库。 通过提供易于使用的混合,它可以帮助您创作Flexbox网格系统和本地媒体查询。 您无需编辑标记即可使其工作-语义取决于您。... // * Modifiers are
用于创建modifiers.json词典的脚本,以方便修饰符。 用法 编辑make-modifiers.py文件,然后将所需的键添加到hotkeys变量中。 例如,将["KH-FG", "grave"],到hotkeys数组中,以创建⌘ `切换窗口的快捷方式。 另请...
Synced Modifiers 是一个 Blender 插件,允许您同时向多个对象添加修改器,并使用驱动程序自动同步所有修改器。 如果你想要免费的blender插件、模型等可以看看这个blender资源库:...
Maya 中的几何分形取一个基础对象并制作它的副本,链接平移、旋转和缩放(变换) ###基于搅拌机阵列修饰符: ://wiki.blender.org/index.php/Doc:2.6/Manual/Modifiers/Generate/Array ####Branching Arrays 在 Maya...
KeyModifiers.Shift也可以直接使用数字4来表示。 RegisterHotKey(Handle, 100, KeyModifiers.Shift, Keys.S); //注册热键Ctrl+B,Id号为101。KeyModifiers.Ctrl也可以直接使用数字2来表示。 RegisterHotKey...
余烬手势修饰符 提供手势作为修饰符的插件。 兼容性 Ember.js v3.16或更高版本 Ember CLI v2.13或更高版本 Node.js v10或更高版本 安装 ember install ember-gesture-modifiers 用法 当前仅提供“平移”修改器。 ...
r GDB/MI Program control r Miscellaneous GDB commands in GDB/MI r GDB/MI Stack Manipulation Commands r GDB/MI Symbol Query Commands r GDB/MI Target Manipulation Commands r GDB/MI Thread Commands r GDB...
Compose Modifiers Playground IDEA插件的源代码 通过此交互式游乐场了解如何使用Jetpack Compose修改器! 可用的修饰符: 背景 边境 夹子 FillMaxHeight FillMaxSize FillMaxWidth 抵消 填充 旋转 规模 阴影 ...
已弃用JSX的事件修饰符:改为查看https://github.com/vuejs/jsx。此babel插件在JSX中添加了一些语法糖。...[” jsx-event-modifiers“,” transform-vue-jsx“]}事件修饰符示例:export default {render(){return
官方离线安装包,亲测可用