Export (0) Print
Expand All

21.2 Member Visibility

Visual Studio .NET 2003

The internal and external visibility for a member of a public class can differ. This is accomplished using pairs of access-specifiers selected from public, protected, and private.

Characteristics

  • When two access-specifiers are used, the most restrictive visibility is the external visibility. Ordering is irrelevant.

    Example

    public __gc class G {
    private public:
       void m1();      // externally private, internally public
    public private:
       void m2();      // externally private, internally public
    };
    
  • If only one access specifier is given, it is identical to giving the same specifier twice.

    Example

    public __gc class G {
    public:
       void m1();      // externally public, internally public
    public public:
       void m2();      // externally public, internally public
    };
    
  • Any of the nine combinations of access specifiers can be used.
    First access specifier Second access specifier Effect
    public
    
    public
    
    Externally and internally public
    public
    
    private
    
    Externally private, internally public
    public
    
    protected
    
    Externally protected, internally public
    private
    
    public
    
    Externally private, internally public
    private
    
    private
    
    Externally and internally private
    private
    
    protected
    
    Externally private, internally protected
    protected
    
    public
    
    Externally protected, internally public
    protected
    
    private
    
    Externally private, internally protected
    protected
    
    protected
    
    Externally and internally protected
  • The visibility of a member outside of its defining assembly can never be greater than the visibility of the class in which it is declared. If an access specifier of a member allows greater visibility, the class access specifier applies instead.

    Example

    private __gc class G1 {
    public protected:
       void m1();      // externally private, not externally protected
    };
    
Show:
© 2015 Microsoft