Effective C + + Notes _ clause 43 Learn to handle names within a templated base class

Source: Internet
Author: User

Here is an example code that describes the name (function) within the templated base class around this sample code. mainly because C + + knows that the base class templates is likely to be special, and that the special version of Ken does not provide the same interface as a generic template. Therefore it often refuses to look for inherited names within the Templatized base classes (templated base class).

1 classCompanyA {2   Public:3      //...4      voidSendcleartext (ConstSTD::string&msg);5      voidSendencrypted (ConstSTD::string&msg);6      //...7  };8 9  classCOMPANYB {Ten   Public: One      //... A      voidSendcleartext (ConstSTD::string&msg); -      voidSendencrypted (ConstSTD::string&msg); -      //... the  }; -  -  //... Classes designed for other companies -   +  /*used to hold information for future information generation*/ -  classMsginfo { +      //... A  }; at  -Template<typename company> - classMsgsender { -  Public: -     //... constructors, destructors, etc. -     voidSendclear (Constmsginfo&info) in     { -STD::stringmsg; to         //generate information based on info + Company C; - C.sendcleartext (msg); the     } *  $     voidSendsecret (Constmsginfo&info)Panax Notoginseng     { -STD::stringmsg; the         //generate information based on info + Company C; A C.sendencryptedtext (msg); the     } + }; -  $ /*log: Record each message sent related records*/ $Template<typename company> - classLoggingmsgsender: PublicMsgsender<company> { -     //... constructors, destructors, etc. the     voidSendclearmsg (Constmsginfo&info) -     {Wuyi  the         //write "pre-transmit" information to log -Sendclear (info);//call the base class function (Error: Different by compiling) Wu         //write the message "post-transmit" to log -     } About  $ }; -  - /*the reason for calling the base class function (Error: Different by compiling): - The template type does not know if company has a sendclear function before compiling, such as the following Companyz class A  */ + classCompanyz { the     //sendcleartext function Not available -     //... $      voidSendencrypted (ConstSTD::string&msg); the      //... the};

In order to solve the above problem: You can use for Companyz, to make a green channel:
1.class definition before adding template<> that: it is neither a template nor a standard class, Instead, a special version of the Msgsender template is used when the template argument is Companyz.
2. Template full specification (total templates specialization): Msgsender is specific to the type Companyz, and its specificity is comprehensive, it is said that once the type parameter is specified as Companyz, No more template parameters are available to change.

 1  template<> //  a fully-msgsender, the same as the generic template, except that it deletes the sendclear  2  class  msgsender<companyz> { 3  //  ...  4  const  msginfo& info)  5   { 6   Sendencryptedtext (info);  7   8  };
1 /*look again, log: Record each message sent related records*/2Template<typename company>3 classLoggingmsgsender: PublicMsgsender<company> {4     //... constructors, destructors, etc.5     voidSendclearmsg (Constmsginfo&info)6     {7 8         //write "pre-transmit" information to log9 Sendclear (info); Ten         /* One If company = = Companyz, Companyz does not exist sendclear function, error.  A reason: The compiler knows that the base class templates is likely to be special, and that the special version of Ken does not provide - the same interface as the generic template. Therefore it often refuses to look for inherited names within the Templatized base classes (templated base class).  -         */ the         //write the message "post-transmit" to log -     } -  -};

The workaround to let C + + "Do not enter templatized base classes observation" is as follows:

1. Add "this->" before the base class function calls the action:

1 /*log: Record each message sent related records*/2Template<typename company>3 classLoggingmsgsender: PublicMsgsender<company> {4     //... constructors, destructors, etc.5     voidSendclearmsg (Constmsginfo&info)6     {7 8         //write "pre-transmit" information to log9          This-Sendclear (info); Ten         //write the message "post-transmit" to log One     } A     //... -};

2. Use using declarative
This is not the base class name that is obscured by the derived class name, but rather the compiler does not enter the base class scope to find it, so you tell it by using it and ask it to do so.

1 /*log: Record each message sent related records*/2Template<typename company>3 classLoggingmsgsender: PublicMsgsender<company> {4     usingmsgsender<company>::sendclear;//tell the compiler that it assumes that sendclear is within the base class5     //... constructors, destructors, etc.6     voidSendclearmsg (Constmsginfo&info)7     {8 9         //write "pre-transmit" information to logTen Sendclear (info);  One         //write the message "post-transmit" to log A     } -     //... -};

3. Make it clear that the function being called is within the base class
Cons: If the virtual function is called, explicit self-grooming (explicit qualification) turns off the "virtual binding behavior"

1 /*log: Record each message sent related records*/2Template<typename company>3 classLoggingmsgsender: PublicMsgsender<company> {4     //... constructors, destructors, etc.5     voidSendclearmsg (Constmsginfo&info)6     {7 8         //write "pre-transmit" information to log9Msgsender<company>:: Sendclear (info); Ten         //write the message "post-transmit" to log One     } A     //... -};

Summary:

1. Three methods are from the perspective of the name (visibility point):

Any special version of the compiler that promises "base class template" will support the interface provided by its generic (generalized) version. [Compiler's early diagnostic time: When parsing the definition of the derived class template]; If this commitment is not practiced, the compiler will eventually give the truth a fair result. [in the compiler late diagnostic time: When those templates are materialized by a specific template]. As in the following example:

1  Loggingmsgsender<companyz> Zmsgsender; 2 Msginfo Msgdata; 3  // ... 4  Zmsgsender.sendclearmsg (msgdata);   // Error

2. Within the derived class templates, "this->" refers to the name of a member within the base class templates, or through a clearly written "base class qualifier modifier".

Disclaimer: Full text is the source of the effective C + + 3th. This is written in their own reading when combing the notes. It is my pleasure to hope that it will be useful to others.

Effective C + + Notes _ clause 43 Learn to handle names within a templated base class

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.