6
2
2014
26

关于模板解读

本文借鉴参考:

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: 1548
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.

Avatar_small
GSEB STD-1 Question 说:
2022年9月10日 21:18

Gujarat State Department of School Education, Gandhinagar and the State level subject experts and other private school teaching staff of the Elementary Level Primary School have designed and suggested the GSEB 1st Class Model Paper 2023 with sample answers Set wide as SET-A, SET-B, SET-C and SET-D to know the new exam scheme or question pattern. GSEB STD-1 Question Paper Students of the Gujarat state can download the NCERT & SCERT Syllabus GSEB STD-1 Question Paper 2023 Pdf with sample answers along with the class teacher’s suggested all lesson or chapter’s most important questions for Part-A, Part-B, Part-C and Part-D exams of Term-1 & Term-2 to the academic year of 2023.

Avatar_small
global news 说:
2023年2月01日 03:34

I curious more interest in some of them hope you will give more information on this topics in your next articles.

Avatar_small
Cladder 说:
2023年2月05日 04:44

Really a great addiction. I have read this great art

Avatar_small
Ower Shelf 说:
2023年3月22日 04:47

It’s a great pleasure reading your post.It’s full of information I am looking for and I love to post a comment that “The content of your post is awesome” Great work.

Avatar_small
Front Door 说:
2023年6月05日 05:06

We are really grateful for your blog post. You will find a lot of approaches after visiting your post. Great work

Avatar_small
Falcon Media Marketi 说:
2023年8月20日 04:15

I was very pleased to find this site.I wanted to thank you for this great read!! I definitely enjoy every little bit of it and I have you bookmarked to check out new stuff you post.

Avatar_small
mg comet ev price 说:
2023年9月15日 06:06

I definitely enjoying every little bit of it. It is a great website and nice share. I want to thank you. Good job! You guys do a great blog, and have some great contents. Keep up the good work.

Avatar_small
how2invest 说:
2023年9月27日 04:36

I was exceptionally satisfied to find this site.I needed to thank you for this extraordinary read!! I most certainly partaking in every single piece of it and I have you bookmarked to look at new stuff you post.

Avatar_small
Podcast 说:
2023年11月05日 05:09

I’m happy to find so many useful info here in the post, thanks for sharing. I love the variety of content available on your website. I’ll bookmark your blog and take the feeds also!


登录 *


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