`
zealotds
  • 浏览: 119721 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

C++ Access Control Modifiers / C++ 访问控制符

    博客分类:
  • c++
阅读更多
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被用于继承关系时可以修改上述声明的访问权为你在此指定的修饰符
  • 上述修改只在当这种修改使得成员访问权“更小”时才会发生

分享到:
评论

相关推荐

    Turbo C++ 3.0[DISK]

    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 方法修饰符

    modifiers是方法修饰符的集合,也是制作更多的方法。 显然,方法修饰符会修改方法。 具体来说,在 Ruby 术语中,它们是类方法,它们: 取一个符号参数,它命名同一个类的实例方法,和返回相同的符号,但导致对命名...

    Java Class and Method Modifiers

    Java Class and Method Modifiers

    glint-template-types

    该程序包包含闪烁所需的类型(临时)。 为了使用此功能,您必须添加 import '@glint/environment-ember-loose...import { DidUpdateModifier } from '@gavant/glint-template-types/types/ember-render-modifiers/did-

    Core C++ A Software Engineering Approach.pdf

    For developers with experience in any language, Victor Shtern&rsquo;s Core C++ teaches C++ the ...modifiers, dynamic memory management, class composition, inheritance, polymorphism, I/O, and much more.

    C++标准库(第二版)英文版.pdf

    The C++ Standard Library A Tutorial and Reference (2nd Edition)+cppstdlib-code.zip C++标准库(第二版)英文版.pdf 非扫描版+源代码 Prefaceto the SecondEdition xxiii Acknowledgments for the Second...

    blender 源代码 c c++ git 2016-4-3同步获取

    avi bmesh freestyle modifiers windowmanager blenfont collada gpu nodes blenkernel compositor ikplugin physics blenlib datatoc imbuf python blenloader depsgraph makesdna quicktime gameengine/ ...

    Turbo C++ 3.00[DISK]

    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 ...

    draft-js-modifiers:Draft.js的模块化状态修饰符

    mergeBlockData ( currentEditorState , { foo : 1 } )Moduler导入 import adjustBlockDepth from 'draft-js-modifiers/adjustBlockDepth'// Support Tree Shaking for webpack, rollup.jsimport { insertText } ...

    hagrid:Hagrid是一个用于响应式网站和Web应用程序的混合库

    Hagrid是一个用于响应式网站和Web应用程序的混合库。 通过提供易于使用的混合,它可以帮助您创作Flexbox网格系统和本地媒体查询。 您无需编辑标记即可使其工作-语义取决于您。... // * Modifiers are

    plover-modifiers:用于创建modifiers.json词典的脚本,以解决不支持修饰符的Plover的问题

    用于创建modifiers.json词典的脚本,以方便修饰符。 用法 编辑make-modifiers.py文件,然后将所需的键添加到hotkeys变量中。 例如,将["KH-FG", "grave"],到hotkeys数组中,以创建⌘ `切换窗口的快捷方式。 另请...

    Blender批量网格模型加效果器插件 Synchronize Modifiers V2.2.0.zip

    Synced Modifiers 是一个 Blender 插件,允许您同时向多个对象添加修改器,并使用驱动程序自动同步所有修改器。 如果你想要免费的blender插件、模型等可以看看这个blender资源库:...

    GeometricFractalsMaya:与制作几何分形相关的 Maya python 脚本

    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-gesture-modifiers:Ember插件,可将手势用作修饰符

    余烬手势修饰符 提供手势作为修饰符的插件。 兼容性 Ember.js v3.16或更高版本 Ember CLI v2.13或更高版本 Node.js v10或更高版本 安装 ember install ember-gesture-modifiers 用法 当前仅提供“平移”修改器。 ...

    Debugging with GDB --2002年5.11

    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

    Compose Modifiers Playground IDEA插件的源代码 通过此交互式游乐场了解如何使用Jetpack Compose修改器! 可用的修饰符: 背景 边境 夹子 FillMaxHeight FillMaxSize FillMaxWidth 抵消 填充 旋转 规模 阴影 ...

    jsx-event-modifiers-一个Babel插件,它向JSX添加类似于Vue模板的事件修饰符。-Vue.js开发

    已弃用JSX的事件修饰符:改为查看https://github.com/vuejs/jsx。此babel插件在JSX中添加了一些语法糖。...[” jsx-event-modifiers“,” transform-vue-jsx“]}事件修饰符示例:export default {render(){return

    perl-Class-Method-Modifiers-2.12-8.el8.noarch(1).rpm

    官方离线安装包,亲测可用

Global site tag (gtag.js) - Google Analytics