C ++: 65 --- local class of special tools and techniques

A local class Overview

  • Class can be defined inside a function, we call such a class as local classes
  • Local class some of the features:
    • The type of a local class definition is only visible in the definition of its scope
    • In actual programming, because local class members must complete definition of interior in the class (local class can not declare a function, then outside the local class is defined, it is wrong), so the complexity of the member function is not recommended to set too high
    • Similarly, in the local class is also permitted to declare static data members, because we can not define such a member

Second, the function can not use variables local class scopes

  • Local class variables access rules:
    • Local class can access only: type outer scope defined names, static variables, enumeration member
    • If a local class definition within a function, the function of the normal local variables can not be changed using a local class
  • E.g:
int a, val;
void foo(int val)
{
    static int si;
    enum Loc { a = 1024, b };

    struct Bar
    {
        Loc locVal; //正确,使用一个局部类型名
        int barVal;

        void fooBar(Loc l = a)
        {
            //barVal = val; //错误,val是foo函数的局部变量
            barVal = ::val; //正确,使用全局变量
            barVal = si;    //正确,使用静态局部变量
            locVal = b;     //正确,使用一个枚举成员
        }
    };
}

Third, the conventional access protection rules apply equally to local class

  • Access rules are as follows:
    • The outer function of local classes do not have any access privileges private members
    • Of course, the local class can be declared as friends outer function ; or a more common situation is that the local class declared as a member of the public
    • In the program have access to local class code is very limited. Local classes have been encapsulated in the function scope, through information hiding nothing further package becomes necessary

Fourth, the local class name lookup

  • Local class internal name lookup order is similar to other classes
  • When members of the class declaration, it must first ensure that the use of the domain name located in the role, and then change the name to use
  • Used names can appear anywhere in the class (but you can not use local variables) defining members:
    • If the name is not a member of a local class, then continue to look at the outer function scope
    • If it is not found, then look at the role of the domain where the outer function

V. nested local class

  • It may then be nested inside a local class class
  • In this case, the definition may be nested class occurs outside the local class. However, a nested class must define the same class in the local scope
void foo()
{
    class Bar
    {
    public:
        class Nested; //声明
    };

    class Bar::Nested {}; //定义
}
  • Nested class is a class in the local local class, you must comply with various regulations of the local class. All members of the nested class must be defined within a nested class
Released 1504 original articles · won praise 1063 · Views 430,000 +

Guess you like

Origin blog.csdn.net/qq_41453285/article/details/104733189