What is the scope of a type declaration inside a class?

If a new class is declared in the class, for example:

class foo {
public :
   struct s1 {
        int a ;
   };
private :
  struct s2 {
        int b ;
  };
};

then in which area can the following expressions be used:

s1 ss1;
s2 ss2;

Thanks in advance.

+3
source share
4 answers

Type s1 can be used anywhere, but if it is used outside the functions of foo members, it must be qualified:

foo::s1 ss1;

Type s2 can only be used in foo member functions.

+7
source

According to your example, both can only be used inside the class foo. However, with the classifier s1can also be used outside foo, for example

foo::s1 ss1;
+1
source

: .

, . , ( , , , ).

: . ( ) , . , .

+1

The scope of the nested class is limited to the wrapper class. These two classes cannot be accessed outside of foo.

However, there is a difference between the classes s1 and s2. You cannot create s2 objects outside of foo.

You could create s1 objects outside of foo using the full name, as in Foo: s1 fs1; Classes that inherit Foo will be able to access s1, but not s2.

+1
source

Source: https://habr.com/ru/post/1733345/


All Articles