Customization of C ++ template: Supplement to custom member functions

zhaozj2021-02-11  167

Customization of C template: Supplement to custom member functions

Author: Yuanxiao Kai

South China University of Technology Computer Research Institute North District R & D

Email - ccplusplus@21cn.com

Foreword

In the article "Customized member function and default class" in the C template, I briefly introduce the customization of member functions in the template class, but there is a detail to be clear, so as not to misunderstand. The member functions in the template class cannot be locally customized in C . Again the usage of template member functions in the template class. I hope everyone has to help.

First, the member function in the custom template class

The member functions in the template class can be customized in the Custom Member Functions and Default Custom Customs Customizable 4: Custom Member Functions and Default Class. At this time, you will not think that the next example should be possible.

Template

Class classname

{

PUBLIC:

ClassName (t t): _ VAL (t) {}

~ classname () {}

Void Display (P);

Private:

T_val;

}

Customized Template class Void Display (P):

Template

Void classname :: Display (char * p)

{

IF (NULL == _ VAL)

COUT << "no content" << endl;

Else

Cout << _ val << endl;

......

}

very sorry! This is not good, I really hope to be able to do it when I start thinking about it. But in fact it is indeed. I don't know why, but I really want to know, if the hero can tell, thank you down! Remember, don't make my equally mistakes. ^ _ ^

Second, template member functions in template class

When we get the template's benefits, we also hope to use the template member function, let's take a brief introduction. Suppose there is a template class that requires an addition function to handle any type.

Template

Class classname

{

Template

I1 Add (i1 i1, i2 i2)

{

Return ... .;

}

}

The ADD member function in the above example is a template member function in a template class. But how do you write when you don't inline? If you try on the VC, you will never get the right result because it does not support. The correct way of writing is as follows:

Template

Template

I1 classname :: add (i1 i1, i2 i2)

{

......

}

This is the Writing of the C standard. Please don't say it. Do you know that it is better than you don't know, isn't it? Ok, don't say it, I hope to help you. The rush of the article, there is a typison or error, please pay more. Welcome to communicate with you.

2001-11-24

转载请注明原文地址:https://www.9cbs.com/read-5572.html

New Post(0)