A
note inheritance may encounter problems when moving from "object-oriented C + +" to "template C + +": because the base class template may be special, and the specific version may change the members, C + + rejects the search for inherited names in the templated base class.
Two
Look at the following example:
Assuming that the information is transferred to different companies, the transfer method includes plaintext and ciphertext transmission, using the design method of the template class:
Class CompanyA {public:...void sendcleartext (const string& msg); void sendencrypted (const string& msg);..}; Class Companyb {public:...void sendcleartext (const string& msg); void sendencrypted (const string& msg);}; ...//other company's Classesclass msginfo {...}; This class holds information for future production Template<typename Company>class Msgsender {public:...void sendclear (const msginfo& info) {string msg;...//generates information based on info company C;c.sendcleartext (msg);} void Sendsecret (const msginfo& info) {...} The call here is c.sendencrypted};
If you want to add the ability to log information when sending information, use inheritance:
Template<typename Company>class loggingmsgsender:public msgsender<company> {public:...void SENDCLEARMSG (const msginfo& Info) {...//writes the pre-transmit information to the record information sendclear (info);//attempts to call the base class member function to complete the send, but not compile. This is because when a derived class is not materialized, it does not know what type of company it is, and it does not know whether the function exists in the base class ...//writes the transmitted information to the record information} ...};
The compiler does not let it compile. At this point the compiler throws a "Sendclear does not exist" complaint. Can sendclear obviously be inside the base class? Depressed, this is why NI? Don't worry, let me explain: When the compiler encounters the Loggingmsgsender definition, because company is a template parameter, not until Loggingmsgsender is present, The compiler has no way of knowing what company is. Because it is impossible to know what company is, it cannot determine what msgsender is, and it cannot know if it has a sendclear function. Stop, you may be explaining to me now that there is a question: why can't you be sure what company is based on template company? The template company is not declarative! Here I propose: We cannot determine the operation of a class with a generic template class based on its declaration, because the template class has an issue with a specific version of the declaration.
Three
Improved method:
(1) Add the this pointer before the base class function calls the action:
Template <typename company> void loggingmsgsender<company>::sendclearmsg (const msginfo& info) { ... This->sendclear (info); Ok ... }
(2) using a using declaration to use a base class namespace within a class:
Template <typename company> class loggingmsgsender:public msgsender<company>{public : // The situation here is not that the base class name is obscured by the derived class name, but that the compiler does not enter the base base //Scope lookup, so we tell it through the using declaration, and ask it to do so using the Msgsender <company>::sendclear;//tells the compiler that it assumes that Sendclear is inside the base class ... void sendclearmsg (const msginfo& info) { ... Sendclear (info);//ok. } };
(3) explicitly qualifying functions are functions in the base class:
Template <typename company> class loggingmsgsender:public msgsender<company>{public : ... void sendclearmsg (const msginfo& info) { ... Msgsender<company>::sendclear (info); Ok ... } ... };
However, there is a clear flaw in this approach: if the virtual function is called, the explicit qualification modifier above will turn off the "virtual binding behavior."
So try to choose the first two methods.
Please remember:
(1) The name of the member within the base class template may be referred to in the derived class template through "this->", or by a clearly written "base class qualifier modifier".
Effective C + +: Clause 43: Learn to handle names within a templated base class