C ++: 63 --- nested class of special tools and techniques

A nested class Overview

  • A class may be defined within another class, the former is referred to as a nested class or nested type

Nested relationship with the outer class Class

  • Nested class is a class independent, with little to do with the outer class . Both are independent (data members of the class to the outer nested class is not directly used for data members nested class outer class is also not directly used)
  • Scope of the relationship:
    • Outer nested class name class scope visible, but is not visible outside the scope of the outer class
    • Nested class name does not conflict with other names scope of access to the relationship:
  • Since a class is nested in the outer member of the class definition, so access to the outer nested class by class for a given set of access rights:
    • If the outer layer is defined in the private class: the outer nested class can be a smart access class and its friend
    • If the outer layer is defined in the protected class: class can be nested outer class and derived classes and their friend access
    • If the outer layer is defined in the public class: nested class can be a random access
  • Internal nested class data members have their own access, so access to the internal members have their nested class defines (private, protected, public)

Second, declare a nested class

  • Here we define a class for TextQuery text queries, its internal QueryResult called nested class objects used to represent the results of the query operation TextQuery
  • The following is internally TextQuery as a nested class declarations
class TextQuery {
public:
    class QueryResult; //嵌套类,此处只是声明并非定义
};

Third, in addition to the outer nested class definition of class

  • Above, we declare a nested class inside TextQuery class, but not yet defined. Similarly, the member function defined Similarly, we can use the following method of externally defined in a nested class TextQuery of:
class TextQuery {
public:
	class QueryResult; //嵌套类的声明
};

//嵌套类的定义
class TextQuery::QueryResult {
    QueryResult(); //构造函数,声明未定义
};

Fourth, the nested class member definitions

  • Here we demonstrate outside a nested class defines a constructor:
class TextQuery {
public:
    class QueryResult;
};

class TextQuery::QueryResult {
public:
    QueryResult(); //构造函数,声明未定义
};

//构造函数的定义
TextQuery::QueryResult::QueryResult() 
{
	
}

Fifth, the definition of a nested class static member

  • Static members can declare and initialize in the class can be declared outside the class initialization class
  • Here we declare a static member in a nested class outside initialized categories:
class TextQuery {
public:
    class QueryResult; //嵌套类声明
};

//嵌套类定义
class TextQuery::QueryResult {
public:
	int static_mem; //声明静态成员
};

//定义静态成员
int TextQuery::QueryResult::static_mem = 1024;

Sixth, the name of the nested class scope to find

  • Find the name of the class rules and the rules of the general class look like, if you want to access external nested class, first class access to the outside, and then accessed via an external class nested class
class TextQuery {
public:
    class QueryResult;        //嵌套类的声明
    QueryResult query()const; //TextQuery的成员函数(必须定义在QueryResult的声明后面
                              //要不然QueryResult会显示未声明
};

//嵌套类的定义
class TextQuery::QueryResult 
{
    //...
};

//TextQuery成员函数的定义
//返回值类型必须使用TextQuery限定符来说明,要不然外部无法识别
TextQuery::QueryResult TextQuery::query()const
{
	//...
}

VII and outer nested classes are independent of each category

  • Although the scope nested classes are defined in the outer class, but the class and the outer nested classes without any relation
    • Nested class objects contain only member of the class definition nested
    • Object class contains only the outer member outer class definition
Released 1504 original articles · won praise 1063 · Views 430,000 +

Guess you like

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