Item43 of Objective C ++ Reading Notes: name of the templated base class

Source: Internet
Author: User

1. When switching from "Object-Oriented C ++" to "template C ++", inheritance may encounter problems:Because the base class template may be made special, and the specific version may change the Member, C ++ refuses to search for the inherited name in the templated base class.

2. Example: assume that the information is transmitted to different companies. The transmission methods include plaintext transmission and ciphertext transmission. The template design method is used:

 
Template <typename company> class msgsender {public :... void sendclear (const msginfo & info) {STD: String MSG ;... // generate Company C; C. sendcleartext (MSG);} void sendsecret (const msginfo & info ){...} // encrypted transfer };

If you want to add the function of recording information when sending information, use the inheritance method:

Template <typename company> class loggingmsgsender: Public msgsender <company> {public :... void sendclearmsg (const msginfo & info ){... // write the information before transfer to the record information sendclear (Info); // try to call the base class member function to complete sending, but cannot pass compilation. This is because you do not know the type of company when the derived class is not activated, so you do not know whether the function exists in the base class... // write the transferred information to record information }...};

Improvement Method:

(1) Add the this pointer before the base class function calls the action:

 
This-> sendclear (Info); // assume that the function is inherited

(2) Use the using declarative class to use the base class namespace:

Using msgsender <company>: sendclear; // tells the compiler to make it assume that the base class contains sendclear

(3) explicitly specifying that a function is a function in the base class:

 
Msgsender <company >:: sendclear (Info); // assume that the function is inherited

3. if a class is similar to a template class, but its structure is different, you can use "Fully-exclusive template". In this way, when the template is specially converted to a specific template parameter type, the special version is called, definition Format:

 
Template <> class msgsender <companyz> {// This version is public when the parameter is of the companyz type :... void sendsecret (const msginfo & info); // a specific version ...};

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.