Previous: http://www.bkjia.com/kf/201112/115030.html
16.4.2 non-type parameters
// Template1.h
# Include <string>
Using namespace std;
# Ifndef TEMPLATE1_H
# Define TEMPLATE1_H
Template <int hi, int wid>
Class Screen {
Public:
Screen (): screen (hi * wid, '#'), cursor (0), height (hi), width (wid ){}
Private:
String screen;
String: size_type cursor;
String: size_type height, width;
};
# Include "Template1.cpp"
# Endif
// Template1.h
# Include <string>
Using namespace std;
# Ifndef TEMPLATE1_H
# Define TEMPLATE1_H
Template <int hi, int wid>
Class Screen {
Public:
Screen (): screen (hi * wid, '#'), cursor (0), height (hi), width (wid ){}
Private:
String screen;
String: size_type cursor;
String: size_type height, width;
};
# Include "Template1.cpp"
# Endif // CP. cpp
Screen <100,200> hp;
// CP. cpp
Screen <100,200> hp; the real parameter of a non-type template must be a regular expression of compilation.
$ Statement in the 16.4.3 class template
In the class template, You can see three types of membership declarations:
(1) A friend declaration of a common non-template class or function, which grants a friend relationship to a class or function explicitly specified.
(2) The membership Declaration of the class template or function template grants access to all instances of the membership.
(3) only a membership statement that grants access to a specific instance of a class template or function template.
1. normal friends
A non-template class or non-template function can be a friend of a class template.
Template <int hi, int wid>
Class Screen {
Public:
Screen (): screen (hi * wid, '#'), cursor (0), height (hi), width (wid ){}
Private:
String screen;
String: size_type cursor;
String: size_type height, width;
Friend class Base;
};
Template <int hi, int wid>
Class Screen {
Public:
Screen (): screen (hi * wid, '#'), cursor (0), height (hi), width (wid ){}
Private:
String screen;
String: size_type cursor;
String: size_type height, width;
Friend class Base;
};
2. Normal template friend relationship
# Include <string>
# Include "Class. h"
# Include "Queue. h"
Using namespace std;
Template <int hi, int wid>
Class Screen {
Public:
Screen (): screen (hi * wid, '#'), cursor (0), height (hi), width (wid ){}
Private:
String screen;
String: size_type cursor;
String: size_type height, width;
Template <class Type> friend class Queue;
};
# Include <string>
# Include "Class. h"
# Include "Queue. h"
Using namespace std;
Template <int hi, int wid>
Class Screen {
Public:
Screen (): screen (hi * wid, '#'), cursor (0), height (hi), width (wid ){}
Private:
String screen;
String: size_type cursor;
String: size_type height, width;
Template <class Type> friend class Queue;
}; Youyuan can be a class template or function template.
3. User relationship of a specific template
In addition to setting all instances in a template as friends, classes can only grant access to specific instances.
# Include <string>
# Include "Class. h"
# Include "Queue. h"
Using namespace std;
Template <int hi, int wid, class Type>
Class Screen {
Public:
Screen (): screen (hi * wid, '#'), cursor (0), height (hi), width (wid ){}
Private:
String screen;
String: size_type cursor;
String: size_type height, width;
Friend class Queue <Type>;
};
# Include <string>
# Include "Class. h"
# Include "Queue. h"
Using namespace std;
Template <int hi, int wid, class Type>
Class Screen {
Public:
Screen (): screen (hi * wid, '#'), cursor (0), height (hi), width (wid ){}
Private:
String screen;
String: size_type cursor;
String: size_type height, width;
Friend class Queue <Type>;
}; 4. Declare dependency
When you grant access to all instances of a given template, you do not need to declare such templates or function templates in the scope. In fact, the compiler treats the object meta Declaration as a class or function declaration.
When you want to limit the relationship between friends and friends of a specific instantiation, you must declare a class or function before it can be used for the membership declaration.
# Include <string>
# Include "Class. h"
Using namespace std;
Template <int hi, int wid>
Class Screen {
Public:
Screen (): screen (hi * wid, '#'), cursor (0), height (hi), width (wid ){}
Private:
String screen;
String: size_type cursor;
String: size_type height, width;
Template <class Type> friend class Queue;
};
# Include <string>
# Include "Class. h"
Using namespace std;
Template <int hi, int wid>
Class Screen {
Public:
Screen (): screen (hi * wid, '#'), cursor (0), height (hi), width (wid ){}
Private:
String screen;
String: size_type cursor;
String: size_type height, width;
Template <class Type> friend class Queue;
};
From xufei96's column