C + +: A derived class accesses the name of a templated base class (Templatized base Class)

Source: Internet
Author: User
Tags functions log

Derived classes inherit the member functions of the templated base class, which are not accessible by default, and are named for the templated base class.

The reason is that the customization of the template may cancel some functions, so that they cannot be accessed by default in order to detect errors at compile time.

Derived classes access templated base classes, including three methods:

1. When calling the base class function, use "this->" to indicate that the class called Is this class, which can be checked at compile time;

2. Use declarations allow you to introduce the functions of a base class to a derived class, which can be checked at compile time;

3. Using display decoration (explicit qualification), it is not recommended that the display decoration will mask virtual dynamic binding;

This example is: derived class, call the function of the base class, rewrite the modification format, output;

Code:

* * * test.cpp * * Created on:2014.04.18 * author:spike//*eclipse CDT, gcc 4.8.1*/#inc  
      
Lude <iostream> #include <string> #include <memory> using namespace std; Class Companycaroline {public:void sendcleartext (const std::string& msg) {std::cout << "Cle  
    Artext: "<< msg << Std::endl; } void sendencrypted (const std::string& msg) {std::cout << "Encrypted:" << msg <<  
    Std::endl;  
      
}  
};  
    struct Msginfo {std::string cleartext;  
std::string encrypted;  
      
}; Template<typename company> class Msgsender {public:void sendclear (const msginfo& info) {s  
        td::string msg = Info.cleartext;  
        Company C;  
    C.sendcleartext (msg);  
        } void Sendsecret (const msginfo& info) {std::string msg = info.encrypted;  
        Company C; C.sendencrYpted (msg);  
      
}  
}; Template<typename company> class Loggingmsgsender:public msgsender<company> {public://using MsgS ender<company>::sendclear;  
        Method two void sendclearmsg (const msginfo& info) {std::cout << "Log Begin:";  
        Sendclear (info); This->sendclear (info); Method one//msgsender<company>::sendclear (info);  
      
      
Method Three, will close the behavior of the virtual binding, do not recommend};  
    int main () {msginfo mi = {"Clear", "Encrypted"};  
    Loggingmsgsender<companycaroline> LMS;  
      
    Lms.sendclearmsg (MI);  
return 0; }

Output:

Log Begin:Cleartext:Clear

Author: csdn Blog spike_king

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/cplus/

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.