Chapter 2 templates and generic programming (9)

Source: Internet
Author: User

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
 


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.