Nested classes and local classes

Nested classes and local class rarely used at work, and may be substituted in the design, described here only briefly.
1) local class
local classes in the class definition is to function. The form:
int = 100 Val;
void foo (int Val)
{

struct Bar
{

};

class Bar::InsideClass//定义InsideClass类
{

};

}
2) nested class
nested class is a class defined in another class. The form:
class Interface
{
public:
   Virtual void Fun () = 0;  
};
 
Template <class T, class P>
Interface * the Person (const T & obj, const P & Arg)
{
    int X;
   class the Local: public Interface
   {
   public:
      the Local (const T & obj, const P & Arg)
         : obj (obj), Arg (Arg) {}
      Virtual void Fun ()
      {
         
      }
   Private:
      T A;
      P B;
   };
   return new new the Local (obj, Arg);
}

Nested class must be declared within the class, but may be defined inside or outside the class. When defining a class nested outside the outer class, other than the layer name of the class must define the name of nested classes.
 Nested class name is visible only in the enclosing class.
 Private members of the class members and friends of the class only have access to, and therefore can not access private members of the enclosing class nested class. Nested class can access the peripheral members of the class (through the object, a pointer or reference).
A good design nested class: nested class should be set to private. Nested class members and methods can be set to public.
Nested class can directly access a static member of the enclosing class, type name (typedef), enumeration values.
 

 

(Internal and external class category)
1.1 Scope
statement nested class position determines the scope of a nested class, that is, it determines that part of the program can create objects nested class.

If the nested class declared in the private part of a class, the only class of the nested class outside can know it. The class above is the case.

If the protective portion nested in the class declaration of a class, for the latter it is visible, is not visible to the outside world. Derived class know that nested class, and you can create this type of object directly.

If you nested in the public portion of the class declaration of a class, which allows the latter to use the derived class and the external world. Then external use, must be added to the outer class qualifier outside the scope of the class, such as: NestedClass use, you should be so defined OutClass :: NestedClass nestedInstance.

Nested scopes same structure and enumerated herein. Many programmers use public enum class provides constant customer use.

The following table summarizes, characterized in scopes nested class, structure, enumeration.

1.2 Access
    declared outside the class nested class does not give access to any external class nested class, did not confer any nested class access to the outer class. The access control of the same general class (private, public, protected).

 
A local class
class may be defined within a function, this class is called local classes, local class definition defines only the type of its scope visible.
You can not use local class variable functions scope.
Local classes have been encapsulated in the function scope, further encapsulation through information hiding nothing becomes necessary.
Can then be nested within a class local class, in which case, the definition may be nested class occurs outside the local class and the local class the same scope.

(C ++ can not be defined in the function function .c ++ functions defined in the class that is a member function.) This class and objects created by it, are limited to the function scope; must implement all the member functions in the class, not get outside the class. Because the nested function is not defined; if containing static data members of the class, this function must be initialized outside; not contain a static function member within the class; category in addition to static local variables can not be used outside of the package defined by its function .

the difference between c ++ internal classes and java inner classes is:
c ++ inner class object is not a pointer external object can not access non-static member outside the class object; java non-static inner class object has a pointer to an external class object, access to external non-static member class object.

A local class Demo sample
int A;
  void Fun ()
  {
  static int S;
  class A
  {
    public:
    void the init (int I) S = {I;}
  };
  A m;
  m.init (10);
  }

Another use of the 2-local classes that is used to achieve the type of transformation
class Interface
{
public:
   Virtual void Fun () = 0;  
};
 
Template <class T, class P>
Interface * MakeAdapter (const T & obj, const P & Arg)
{
    int X;
   class the Local: public Interface
   {
   public:
      the Local (const T & obj, const P & Arg)
         : obj (obj), Arg (Arg) {}
      Virtual void Fun ()
      {
          X = 100;
         obj .CALL (Arg );
      }
   Private:
      T obj ;
      P Arg
;
   };
   return the Local new new (obj, Arg);
}

. Embedded class
is another class in the class definition of the body. Outside class called peripheral category. The class and objects created by it, are confined to the outer class scope;

Can I use outside the inner class is all about access; if using, in order to set the inner peripheral class :: class format to use;

Another class is a combination between the kind of evolutionary relationship, i.e. not combined or aggregated produced in other members of the class, but the class is generated in the inner layer; inner class member functions, may be implemented within the class, the class may be outside of the outer layer achieve;

Member function of the inner class does not have access to members of the enclosing class, and vice versa

The purpose of defining nested class that hides the class name, reduced overall identifier, thereby limiting the ability to use the user to create an object class. This can increase between the main abstraction class and two classes emphasized (peripheral nested classes and classes) from the relationship.

1 The following is an example of a nested class
class A
  {
   public:
    class B
    {
    public:
      ...
    Private:
      ...
    };
    void F ();
    Private:
    int A;
  }
wherein a nested class is class B, class A is a peripheral class a, class B definition of class a in vivo.

Several classes of nested Note 2:

1, from the perspective of the scope, nested class is hidden, the class name is used only in the periphery of the periphery of the class in the class. If you use the class name in the role of the enclosing class domain, you need to add the name of limitation.
2, from the point of view of access rights, nested class name and the object name members of its enclosing class have the same access rules. Objects can not access private member functions nested class, and can not create an object on the private part of the enclosing class nested class.
3, a nested class member functions can be defined in its class in vitro.
4, the peripheral members are not members of the class of objects in a nested class described, and vice versa. Nested class member functions do not have access to members of the enclosing class, and vice versa. This country, in the analysis of the relationship between access nested class members with peripherals and the like, tend to be seen as non-nested class nested classes to handle.

The above-described nested class can be written in the following format:

A class
  {
  public:
    void F ();
  Private:
    int A;
  };
 
    class B
  {
    public:
    ...
  Private:
    ...
  };
a primer seen, nested classes are merely embedded in the grammar.

5, a friend described nested class does not have access to members of the enclosing class.
6, if the complex nested classes, nested classes can only be described in the enclosing class, detailed contents about the nested class can be defined in the peripheral domain file vitro.

Guess you like

Origin blog.51cto.com/8413723/2429915