Simple Template Concepts

Source: Internet
Author: User
Tags class definition

I previously on the understanding of the template is intermittent, so prepare the system of the dedicated research, thought to spend a day or two, did not expect, less than a morning, completed the plan, look back, the template is nothing more than that! So, this topic is called "Simple template concept."

Aside from other books and materials, I will describe the template in a simple language as I understand it.

(Of course, actually the template has its own complex side, hehe, I do not discuss this)

1. The role of the template <?xml:namespace prefix = o ns = "Urn:schemas-microsoft-com:office:office"/>

Hum! Why is a Class A function can only use a fixed data type? I just different types, but the processing is exactly the same ah, so the code repetition rate greatly increased AH! Did you ———— wrong?

This is my previous indignant thing, until I found the template, hehe, that is a good thing. It can increase the rate of code reuse. It uses a neutral type to define a class or function, and then replace them with a specific type when used.

N, to reduce code redundancy and the rate of word reuse, here's an example of the following two sections.

2. Class template

The use of templates in the definition of a class is our most-common, you see, there are ATL, there are STL, hehe, a lot.

Off--key word (chow tone): template

Key symbol:<>

Say the key is the key, the top two less can not, how to use it? Read the following simple example and you will soon:

#include <iostream.h>
template <class T>
class myclass{
T temp;
public:
myclass(T name){
temp=name;
}
  T vomit(){
return temp;
}
};
  void main()
{
myclass<int> t1(2);
cout<<t1.vomit()<<endl;
myclass<char*> t2("醉拳");
cout<<t2.vomit()<<endl;
myclass<float> t3(3.14159);
cout<<t3.vomit()<<endl;
}

Do not tell me that you do not understand, otherwise, test down to do it again, don't tell me how you will not do AH! See no, precede the definition of the usual class with Template<class t>, so that T is the type that can be used in this class. When used, the class name and T must be used together to correctly represent the class and replace the T with a certain type, like the myclass<int>,myclass<char*> written above. OK, that's simple.

Note function vomit (who can say its English meaning?) OH), here is implemented with the inline function, then put him outside the class? What's the definition? Oh, just like the following this son AH:

template <class T>
T myclass<T>::vomit(){
return temp;
}

First, with the same template <class T> as the class definition, the class name must be noted for myclass<t> rather than MyClass.

OK, the class template knows this is fine.

3. Function template

Like a class template, a function can also use many types to implement code reuse, for example, do not say more, see for yourself, or run, I have a very simple example, designed to reveal the principle:

#include <iostream.h>
template <class T>
void f(T qie)
{
cout<<qie<<endl;
}
  #define X f<char*>
  void main()
{
f<int>(3);
f<char>('r');
X("aadf");
}

Run a look?

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.