2009年12月8日星期二

类模板与模板类

原创  类模板与模板类

1.类模板与模板类的概念

什么是类模板 一个类模板(也称为类属类或类生成类)允许用户为类定义一种模式,使得类中的某些数据成员、默写成员函数的参数、某些成员函数的返回值,能够取任意类型(包括系统预定义的和用户自定义的)。

  如果一个类中数据成员的数据类型不能确定,或者是某个成员函数的参数或返回值的类型不能确定,就必须将此类声明为模板,它的存在不是代表一个具体的、实际的类,而是代表着一类类。

类模板定义 定义一个类模板,一般有两方面的内容:

A.       首先要定义类,其格式为:

template <class T>

class foo

{

……

}

foo 为类名,在类定义体中,如采用通用数据类型的成员,函数参数的前面需加上T,其中通用类型T可以作为普通成员变量的类型,还可以作为conststatic成员变量以及成员函数的参数和返回类型之用。例如:

template<class T>

class Test{

private:

    T n;

    const T i;

    static T cnt;

public:

    Test():i(0){}

    Test(T k);

    ~Test(){}

    void print();

    T operator+(T x);

};

B.       在类定义体外定义成员函数时,若此成员函数中有模板参数存在,则除了需要和一般类的体外定义成员函数一样的定义外,还需在函数体外进行模板声明

例如

template<class T>

void Test<T>::print(){

    std::cout<<"n="<<n<<std::endl;

    std::cout<<"i="<<i<<std::endl;

    std::cout<<"cnt="<<cnt<<std::endl;

 

}

如果函数是以通用类型为返回类型,则要在函数名前的类名后缀上"<T>"。例如:

template<class T>

Test<T>::Test(T k):i(k){n=k;cnt++;}

template<class T>

T Test<T>::operator+(T x){

               return n + x;

               }

C.       在类定义体外初始化const成员和static成员变量的做法和普通类体外初始化const成员和static成员变量的做法基本上是一样的,唯一的区别是需在对模板进行声明,例如

template<class T>

int Test<T>::cnt=0;

template<class T>

Test<T>::Test(T k):i(k){n=k;cnt++;}

类模板的使用 类模板的使用实际上是将类模板实例化成一个具体的类,它的格式为:类名<实际的类型>

  模板类是类模板实例化后的一个产物。说个形象点的例子吧。我把类模板比作一个做饼干同的模子,而模板类就是用这个模子做出来的饼干,至于这个饼干是什么味道的就要看你自己在实例化时用的是什么材料了,你可以做巧克力饼干,也可以做豆沙饼干,这些饼干的除了材料不一样外,其他的东西都是一样的了。

2.类模板的派生

  可以从类模板派生出新的类,既可以派生类模板,也可以派生非模板类。派生方法:

⑴ 从类模板派生类模板可以从类模板派生出新的类模板,它的派生格式如下例所示:

template <class T>

class base

{

……

};

 

template <class T>

class derive:public base<T>

{

……

};

与一般的类派生定义相似,只是在指出它的基类时要缀上模板参数,即base<T>

⑵ 从类模板派生非模板类  可以从类模板派生出非模板类,在派生中,作为非模板类的基类,必须是类模板实例化后的模板类,并且在定义派生类前不需要模板声明语句:template<class>。例如:

template <class T>

class base

{

……

};

 

class derive:public base<int>

{

……

};

在定义derive类时,base已实例化成了int型的模板类。

 

12 条评论:

匿名 说...

I drop a leave a response when I like a post on a website or if I have
something to add to the discussion. Usually it's a result of the passion communicated in the article I looked at. And after this post "类模板与模板类". I was actually moved enough to post a comment :-P I do have some questions for you if it's
okay. Could it be simply me or do some of the comments come across like written by brain dead people?
:-P And, if you are posting on additional online sites, I would like to keep up with everything fresh you
have to post. Could you list all of your communal pages like
your linkedin profile, Facebook page or twitter
feed?

Have a look at my blog post ... Order Natural Cleanse

匿名 说...

Cool blog! Is your theme custom made or did you download it from somewhere?
A design like yours with a few simple tweeks would really make my blog
jump out. Please let me know where you got your design. Kudos

Check out my page :: Authrntic green coffee review

匿名 说...

I seldom drop responses, but i did some searching and
wound up here "类模板与模板类".
And I actually do have a couple of questions for you if it's allright. Is it just me or does it appear like some of the responses look as if they are written by brain dead people? :-P And, if you are posting on additional online social sites, I'd like to follow
anything fresh you have to post. Could you make a list of every one of your communal sites
like your twitter feed, Facebook page or linkedin profile?


Also visit my web page ... pure raspberry ketones

匿名 说...

I always emailed this webpage post page to all my contacts, since if
like to read it then my links will too.

Also visit my web page: jobs on line form home

匿名 说...

Hi Dear, are you genuinely visiting this
web page on a regular basis, if so after that
you will definitely obtain nice experience.

Here is my web-site :: payday Loan

匿名 说...

Keep this going please, great job!

raspberry ketone reviews

匿名 说...

Pretty component of content. I just stumbled upon your blog and in accession capital to say that I get in fact loved account your weblog posts.
Anyway I will be subscribing for your feeds or even I
achievement you get right of entry to consistently rapidly.


My blog post garcinia cambogia hca

匿名 说...

Peculiar article, just what I was looking for.

123 income academy reviews

匿名 说...

ll need to consider your OSSF options, as no government system will be providing
any sewage amenities to your building. Native American Indians used buffalo
droppings for this purpose, and the indigenous peoples on the continents of Africa,
Europe, the Middle East, and Asia do the same for poop scooped up
from the animals with which they share the land.

If you are purchasing a waste water treatment system for either commercial or domestic purposes then there
are a few things that you will need to take into consideration in order to choose the right system for your needs.


Feel free to visit my web page ... echanwallpapers.info; ,

匿名 说...

I am really loving the theme/design of your weblog. Do you ever run into any browser compatibility problems?

A number of my blog visitors have complained about my website not operating correctly in Explorer but looks great in Opera.
Do you have any solutions to help fix this problem?

Stop by my weblog low-cut basketball

匿名 说...

Right here with the final word is definitely Hollywood's Shania Clooney:
I demand how to hack a facebook account for free, nothing more nothing less.

Almost every computer user now is registered on at least one or another social network.
As Apple is known as a computer company, consumers considered
the launch of the i - Phone merely a marketing and business strategy to increase the revenues on a short term from
na.

Feel free to visit my page: pirater un compte facebook gratuitement

匿名 说...

Your style is really unique compared to other
folks I've read stuff from. Many thanks for posting when you have the opportunity, Guess I'll just bookmark this site.


my page christian louboutin milano