I am trying to inherit a "derived" class from either base1 or base2. I want to use template specialization for this purpose. I have the following
//base1
template<typename FT>
class base1
{
public:
base1(FT m)
{}
};
//base2
template<typename FT>
class base2
{
public:
base2(FT m)
{}
};
//define template specializations
template<int sType, typename FT>
class baseT {};
//for base1
template<typename FT>
class baseT<1,FT>
{
public:
typedef base1<FT> bType;
};
//for base2
template<typename FT>
class baseT<2,FT>
{
public:
typedef base2<FT> bType;
};
//Derived class which inherits either from base1 or base2
template<int sType, typename FT>
class derived : public baseT<sType,FT>::bType
{
public:
derived(FT m)
{}
};
int main( )
{
derived<1,float> a1(1);
return 0;
}
This code is not compiling (on Linux platform) because the base class expects one argument but I am not sure how to pass it to the constructor of derived to make this work. (I dont want to use conditional). Any advice will help
//Derived class which inherits either from base1 or base2
template<int sType, typename FT>
class derived : public baseT<sType,FT>::bType
{
public:
derived(FT m) : baseT<sType,FT>::bType(m)
{}
};
That's all you need to make it work. Initialize the base class.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.