Visibility of class members?

I think I know accessibilty, but I'm not sure how I understand visibility very clearly

For example:

class X { int x; }; 

Here "x" is visible only in the class and is accessible outside the class. If I'm right, can someone explain the text in this answer about how visibility is not controlled, etc.?

(C ++ 03 / 11.0) It should be noted that this is access to members and base classes that are controlled, not their visibility. Member names are still visible and implicit conversions to base classes are still if these members and base classes are not available. The interpretation of this design is established without regard to access control. If the established interpretation uses inaccessible member names or base classes, the structure is poorly formed.

+7
source share
3 answers

Perhaps this example helps:

 class Bob { private: int foo(int, int); }; class David : Bob { void goo() { int a = foo(1, 2); // #1 } }; class Dani : Bob { void foo(); void goo() { int a = foo(1, 2); // #2 } }; 

The name foo displayed on line # 1, but the function that he calls is not available (due to the fact that it is closed to Bob ). This is a compilation error, but the compiler knows that there is a potential Bob::foo function that will match but not be available.

On line # 2, the name foo refers only to Dani::foo , and Bob::foo not visible (because it is hidden), and therefore there is simply no corresponding function to call foo(1, 2) . This is also a compilation error, but this time the error is that there is no function to call.

+7
source

C ++ has some esoteric function related to the visibility and accessibility of member names of a private class. By definition, the name of a member of a private class is accessible only to class members and friends. However, the visibility rule can confuse many. They can be summarized as follows.

  • The private member name is available only to other members and friends.
  • A private member is displayed for all code that sees the class definition. This means that its parameter types must be declared, even if they are never needed in this translation unit ...
  • Overload permission occurs before availability check.

In C ++ today ("C ++ 03" and earlier), the concepts of accessibility and visibility are independent. Members of classes and namespaces are visible whenever they are "to scale", and there is no mechanism to reduce this visibility from the point of declaration. Accessibility is only a parameter for class members and is orthogonal to the concept of visibility. The latter observation is often surprising for novice C ++ programmers. See PDF .

Consider the following example.

 #include < complex> class Calc { public: double Twice( double d ); private: int Twice( int i ); std::complex Twice( std::complex c ); }; int main() { Calc c; return c.Twice( 21 ); // error, Twice is inaccessible } 

When the compiler needs to solve a function call, it performs three main functions in order to:

  • Before doing anything else, the compiler searches for a region that has at least one object named Twice and compiles a list of candidates. In this case, a name search is first looked up in the Calc area to see if there is at least one function named Twice; if not, the base classes and the encompassing namespaces will be examined one at a time, one at a time, until a volume having at least one candidate is found. In this case, however, the very first area that the compiler is looking at already has an entity named Twice - in fact, it has three of them, and so the trio becomes a set of candidates. (For more information on the name of a search in C ++, with a discussion of how this affects how you should package your classes and their interfaces

  • Further, the compiler performs overload resolution to select a unique best match from the candidate list. In this case, the argument is 21, which is int, and the available overloads occupy the double, int, and complex. Obviously, the int parameter is the best match for the int argument (this is an exact match and no conversions are required), and therefore Twice (int) is selected.

  • Finally, the compiler performs an availability check to determine whether the selected function can be called.

Note that accessibility (defined by modifiers in C ++) and visibility are independent. Visibility is based on C ++ scope rules. An element of a class can be both visible and inaccessible.

As an example, static elements are displayed all over the world while your application is running, but are only available with respect to the modifier applied to them.

+7
source

As a side note: when you declare a class , the scope is closed by default (opposite to struct , where members are by default public.)

The member of the variable "x" is available only to your class and its friends. No one else can access the "x" directly (this can indirectly if you have a function that returns a link to it, which is a very bad idea.)

The text you are quoting indicates visibility for the compiler, so X::x exists, no matter what. It will not disappear just because it is private. Visibility is used to find the member you are referring to, and the first match is returned. At this point, the compiler checks for availability, if it is available, you're fine. If not, it is poorly formed.

Notice that I mentioned friends. This keyword makes all member variables available. When the compiler deals with friends, it completely ignores all protected and private keywords.

In most cases, this is a very simple process. He's getting in order. Period.

Where it becomes more complicated, you start using virtual functions: they can be made public, protected and private, and this can change depending on the class declaration ... (A comes from B and makes a publicly protected virtual function, this is not at all a very good idea, but C ++ does not stop you from doing this.) Of course, this applies only to functions, and not to the elements of a variable, therefore another object.

+1
source

All Articles