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/