c++ using declaration, scope and access control

后端 未结 3 1437
深忆病人
深忆病人 2020-12-09 19:55

Typically the \'using\' declaration is used to bring into scope some member functions of base classes that would otherwise be hidden. From that point of view it is only a me

相关标签:
3条回答
  • 2020-12-09 20:08

    With regard to your declaration without using: These are called "access declarations", and are deprecated. Here is the text from the Standard, from 11.3/1:

    The access of a member of a base class can be changed in the derived class by mentioning its qualified-id in the derived class declaration. Such mention is called an access declaration. The effect of an access declaration qualified-id; is defined to be equivalent to the declaration usingqualified-id; [Footnote: Access declarations are deprecated; member using-declarations (7.3.3) provide a better means of doing the same things. In earlier versions of the C++ language, access declarations were more limited; they were generalized and made equivalent to using-declarations - end footnote]

    I would say that most often it's not good to change public members to private or protected members in the derived class, because this will violate the substitution principle: You know a base class has some functions, and if you cast to a derived class then you expect those functions to be callable too, because the derived class is-a base. And like you already mentioned, this invariant is already enforced anyway by the language allowing to convert (which working implicitly!) to a base class reference, or qualifying the function name, and then calling the (then public) function.

    If you want to forbid someone calling a set of functions of the base, then i think this hints that containment (or in rare cases, private inheritance) is a better idea.

    0 讨论(0)
  • 2020-12-09 20:13

    While the using declaration you showed does provide a mechanism to change access level (but only down), that is not the primary use of it in such a context. A using context there is primarily intended to allow access to functions that would otherwise be shadowed from the base class due to the language mechanics. E.g.

    class A {
    public:
       void A();
       void B();
    };
    
    class B {
    public:
       using A::B;
       void B(int); //This would shadow A::B if not for a using declaration
    };
    
    0 讨论(0)
  • 2020-12-09 20:13

    The declaration

    using C::a
    

    brings "a" to the local naming scope so that you can later use "a" to refere to "C::a"; since that, "C::a" and "a" are interchangeable as long as you don't declare a local variable with name "a".

    The declaration does not change access rights; you can access "a" in the subclass only because "a" is not private.

    0 讨论(0)
提交回复
热议问题