问题
Possible Duplicate:
Generating Structures dynamically at compile time
I am now facing a situation where I want a derived class to inherit from either Base1
or Base2
depending on a condition (in C++03). This means, I want to implement something like:
// pseudo-C++ code
class Derived : public
if(condition) Base1 // inherit from Base1, if condition is true
else Base2 // else inherit from Base2
{ /* */ };
This is likely not a good design, but the real world is not perfect.
I have searched here for an answer, but I do not want to use a preprocessor directive Problems with ifdef based inheritance in C++.
How else could I achieve this?
回答1:
I figured out the solution using templates and partial specialization. The below code does the trick:
// provide both the required types as template parameters
template<bool condition, typename FirstType, typename SecondType>
class License {};
// then do a partial specialization to choose either of two types
template<typename FirstType, typename SecondType>
class License<true, FirstType, SecondType> {
public: typedef FirstType TYPE; // chosen when condition is true
};
template<typename FirstType, typename SecondType>
class License<false, FirstType, SecondType> {
public: typedef SecondType TYPE; // chosen when condition is false
};
class Standard {
public: string getLicense() { return "Standard"; }
};
class Premium {
public: string getLicense() { return "Premium"; }
};
const bool standard = true;
const bool premium = false;
// now choose the required base type in the first template parameter
class User1 : public License<standard, Standard, Premium>::TYPE {};
class User2 : public License<premium, Standard, Premium>::TYPE {};
int main() {
User1 u1;
cout << u1.getLicense() << endl; // calls Standard::getLicense();
User2 u2;
cout << u2.getLicense() << endl; // calls Premium::getLicense();
}
The syntax looks unclean, but the result is cleaner than using preprocessor directive.
来源:https://stackoverflow.com/questions/13779938/conditionally-inheriting-from-either-of-two-classes