6
2
2014
17

关于模板解读

本文借鉴参考:

http://www.cnblogs.com/gaojun/archive/2010/09/10/1823354.html

1.概念

模板的好处:实现代码的重用。

如何实现?:把类型定义为参数。

下面举例说明好处。若返回a,b之间较大的数,由于a,b类型不确定,故可能要定义多个函数。如:

//函数1.

int max(int x,int y);
{return(x>y)?x:y ;}

//函数2.
float max( float x,float y){
return (x>y)? x:y ;}

//函数3.
double max(double x,double y)
{return (c>y)? x:y ;}

如果主函数调用max(a,b).而a、b是char类型的,则程序会出错。因为定义的三个类型中没有char的比较。于是就能用模板来解决这个问题

2.函数的模板

函数模板形式

Template<class T或者typename T>

返回值类型 函数名 (形参表)

{ //函数定义}

以下程序

#include <iostream>

using std::cout;

using std::endl;

//声明一个函数模版,用来比较输入的两个相同数据类型的参数的大小,class也可以被typename代替,

//T可以被任何字母或者数字代替。

template <class T>

T min(T x,T y)

{ return(x<y)?x:y;}

void main( )

{

     int n1=2,n2=10;

     double d1=1.5,d2=5.6;

     cout<< "较小整数:"<<min(n1,n2)<<endl;

     cout<< "较小实数:"<<min(d1,d2)<<endl;

     system("PAUSE");

}

这样就可以通过模板将函数定义一次。

3.类模板

定义一个类模板:

Template(class T或者typename T)

class 类名{

//类定义};

看如下例子

// ClassTemplate.h
#ifndef ClassTemplate_HH

#define ClassTemplate_HH

template<typename T1,typename T2>

class myClass{

private:

     T1 I;

     T2 J;

public:

     myClass(T1 a, T2 b);//Constructor

     void show();

};

//这是构造函数

//注意这些格式

template <typename T1,typename T2>

myClass<T1,T2>::myClass(T1 a,T2 b):I(a),J(b){}

//这是void show();

template <typename T1,typename T2>

void myClass<T1,T2>::show()

{

     cout<<"I="<<I<<", J="<<J<<endl;

}

#endif

// Test.cpp

#include <iostream>

#include "ClassTemplate.h"

using std::cout;

using std::endl;

void main()

{

     myClass<int,int> class1(3,5);

     class1.show();

     myClass<int,char> class2(3,'a');

     class2.show();

     myClass<double,int> class3(2.9,10);

     class3.show();

     system("PAUSE");

}

 

Category: 面向对象 | Tags: | Read Count: 1213
Avatar_small
Digital Ali 说:
2021年9月13日 19:40

Thanks for your insight for your fantastic posting. I’m glad I have taken the time to see this. soaptoday

Avatar_small
Digital Ali 说:
2021年9月15日 17:05

Positive site, where did u come up with the information on this posting? I'm pleased I discovered it though, ill be checking back soon to find out what additional posts you include. CBD cream for pain in 2021

Avatar_small
Digital Ali 说:
2021年9月15日 19:21 I found this is an informative and interesting post so i think so it is very useful and knowledgeable. I would like to thank you for the efforts you have made in writing this article. 123movie
Avatar_small
SEO 说:
2021年10月01日 19:42

I recently came across your blog and have been reading along. I thought I would leave my first comment. I don't know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often. more hints

Avatar_small
best ergonomic chair 说:
2021年10月02日 23:01
Be careful while buying a chair for back pain. Here we suggest you best ergonomic chair for watching TV. These are highly recommended chairs. Chose a chair and get rid of your back pain.
 
Avatar_small
SEO 说:
2021年10月10日 00:29

I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post. SEO ติดหน้าแรก

Avatar_small
Mich Rose 说:
2021年10月24日 04:29

What makes a good laptop for Cyber Security? Reviewer Mate presents the ULTIMATE guide to buying the Best Laptop for Cyber Security.

https://reviewermate.com/best-laptop-for-cyber-security/

Avatar_small
SEO 说:
2021年10月31日 14:38

Very efficiently written information. It will be beneficial to anybody who utilizes it, including me. Keep up the good work. For sure i will check out more posts. This site seems to get a good amount of visitors. https://bandarqpkv.web.app/

Avatar_small
bestmotherboardz 说:
2021年11月07日 17:03

bestmotherboardz helps you improve your system and stay one step ahead when playing with multiple contenders.

Avatar_small
bestlaptopviews 说:
2021年11月08日 05:57

bestlaptopviews we provide powerful, light and with a high screen resolution, therefore, we are talking about a minimum of 4-8 GB of RAM, Intel Core i5 or i7 processors and a minimum resolution of 1920 x 1080 pixels.

Avatar_small
MakSitesLike 说:
2021年11月09日 04:06

MakSitesLike We Provide You With The New Latest Breaking News: And Videos Straight From The Entertainment Industry.

Avatar_small
miichellle.rossee 说:
2021年11月14日 16:46

Reliable, affordable and flexible veeam fedramp compliant protection with Off-Site Cloud Connect Backup Repository to cover your security threats available from Sora Solutions Services. Get started today. Visit https://welcome.sora-solutions.services/en/veeam/ for more information.

Avatar_small
Website 说:
2021年12月09日 03:35

HGH injections to lose weight are essential in hormonal growth, weight and strength. For enhancement and overcoming deficiency we need growth hormonal injections and therapy. Our specialists at HGH MD will be glad to discuss your hormone treatment alternatives with you. For a consultation call 888-763-4221 today!

Avatar_small
Website 说:
2021年12月13日 21:10

Sora Solutions Services brings the complete <a href='https://sora-solutions.services/en/development.html'>project management for app development</a> to the table for customer application development and planning.

Avatar_small
Website 说:
2021年12月13日 21:10

Sora Solutions Services brings the complete project management for app development to the table for customer application development and planning.

Avatar_small
Website 说:
2021年12月14日 03:34

When you're looking for a new mountain bike, these best women's mountain bike under 500 will shock you - they're a great value buy and superbly designed. Find your perfect bike with our buyer's guide.

Avatar_small
Bhulekh Odisha 说:
2022年8月04日 00:40

Bhulekh Odisha, a government website that looks after the records of lands in the districts of Odisha State. This website is free to access and the citizens of Odisha state can access the information through their plot number, tenant number, or Khata number. Bhulekh Odisha The duties of landowners, value of a property, property transactions, a record of rights, land map, Bhulekh Odisha, a government website that looks after the records of lands in the districts of Odisha State. This website is free to access and the citizens of Odisha state can access the information through their plot number, and other important information of land will be available on this website. This Portal is a one-stop solution to check the details of land (not related to property tax) if you’re on a way to buy it from some unknown owner.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter

Host by is-Programmer.com | Power by Chito 1.3.3 beta | Theme: Aeros 2.0 by TheBuckmaker.com