`
zealotds
  • 浏览: 122369 次
  • 性别: 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 ...

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

    C 程序设计教学课件:Chapter 3 class and object.ppt

    Access control modifiers play a crucial role in managing how class members can be accessed. Public members can be accessed from anywhere, providing an interface for interacting with the object. ...

    java的面向对象编程

    8. **访问控制修饰符(Access Control Modifiers)**:如前所述,Java提供了`public`, `private`, `protected`和默认(包级私有)四种访问控制修饰符,用于限制类、方法和字段的访问范围。 9. **接口(Interface)*...

    Professional C# 3rd Edition

    Code Access Permissions and Permissions Sets 399 Policy Levels: Machine, User, and Enterprise 403 Support for Security in the Framework 405 Demanding Permissions 406 Requesting Permissions 407 ...

    c-programming-for-scientists-and-engineers-manufacturing-engineering-series

    In today’s technological landscape, C programming remains a cornerstone for many engineering and scientific applications despite the rise of more modern languages like C++. The book "C Programming ...

    Debugging with GDB --2003年6.0

    Table of Contents Summary of gdb . . . . . . . . ....Free software ....Free Software Needs Free Documentation ....Contributors to gdb....1 A Sample gdb Session ....2 Getting In and Out of gdb ....2.1 Invoking gdb ....

    Debugging with GDB --2001年5.3

    Table of Contents Summary of GDB . . . . . . . . ....Free software ....Free Software Needs Free Documentation ....Contributors to GDB ....1 A Sample GDB Session ....2 Getting In and Out of GDB ....Invoking GDB ....

Global site tag (gtag.js) - Google Analytics