C++ 模板类的多重继承

C++ 模板类的多重继承,c++,inheritance,templates,multiple-inheritance,C++,Inheritance,Templates,Multiple Inheritance,我遇到了来自同一模板类的不同实例化的多重继承问题。具体来说,我正在尝试这样做: template <class T> class Base { public: Base() : obj(NULL) { } virtual ~Base() { if( obj != NULL ) delete obj; } template <class T> T* createBase() {

我遇到了来自同一模板类的不同实例化的多重继承问题。具体来说,我正在尝试这样做:

template <class T>
class Base
{

public:

    Base() : obj(NULL)
    {
    }

    virtual ~Base()
    {
        if( obj != NULL ) delete obj;
    }

    template <class T>
    T* createBase()
    {
        obj = new T();

        return obj;
    }

protected:

    T* obj;

};

class Something
{
    // ...
};

class SomethingElse
{
    // ...
};

class Derived : public Base<Something>, public Base<SomethingElse>
{

};

int main()
{
    Derived* d = new Derived();
    Something* smth1 = d->createBase<Something>();
    SomethingElse* smth2 = d->createBase<SomethingElse>();

    delete d;

    return 0;
}
模板
阶级基础
{
公众:
Base():obj(NULL)
{
}
虚拟~Base()
{
如果(obj!=NULL)删除obj;
}
模板
T*createBase()
{
obj=新的T();
返回obj;
}
受保护的:
T*obj;
};
分类
{
// ...
};
给某人上课
{
// ...
};
派生类:公共基、公共基
{
};
int main()
{
派生*d=新派生();
Something*smth1=d->createBase();
SomethingElse*smth2=d->createBase();
删除d;
返回0;
}
当我尝试编译上述代码时,会出现以下错误:

1>[...](41) : error C2440: '=' : cannot convert from 'SomethingElse *' to 'Something *'
1>        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>        [...](71) : see reference to function template instantiation 'T *Base<Something>::createBase<SomethingElse>(void)' being compiled
1>        with
1>        [
1>            T=SomethingElse
1>        ]
1>[...](43) : error C2440: 'return' : cannot convert from 'Something *' to 'SomethingElse *'
1>        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>[…](41):错误C2440:“=”:无法从“SomethingElse*”转换为“SomethingElse*”
1> 指向的类型是不相关的;转换需要重新解释转换、C样式转换或函数样式转换
1> […](71):请参阅对正在编译的函数模板实例化“T*Base::createBase(void)”的引用
1> 与
1>        [
1> T=某物
1>        ]
1> […](43):错误C2440:“返回”:无法从“Something*”转换为“SomethingElse*”
1> 指向的类型是不相关的;转换需要重新解释转换、C样式转换或函数样式转换
由于成员obj同时从Base和Base继承而来,问题似乎很模糊,我可以通过消除对createBase调用的歧义来解决这个问题:

Something* smth1 = d->Base<Something>::createBase<Something>();
SomethingElse* smth2 = d->Base<SomethingElse>::createBase<SomethingElse>();
Something*smth1=d->Base::createBase();
SomethingElse*smth2=d->Base::createBase();
然而,从语法上讲,这个解决方案是非常不切实际的,我更喜欢更优雅的。此外,我对第一条错误消息感到困惑。这似乎暗示在Base中有一个实例化createBase,但这怎么可能呢?如果您能提供有关此问题的任何信息或建议,我们将不胜感激

这似乎意味着在
Base
中有一个实例化
createBase
,但这怎么可能呢

当然有,因为您的
createBase()
是一个成员模板函数(此函数中的
T
与周围类中的
T
无关)

我会这样做:

// in Derived, or you could make some class (eg. MultiBase) for it

template <class T>
T* createBase()
{
  return Base<T>::createBase();
}
//在派生中,或者您可以为其创建一些类(例如MultiBase)
模板
T*createBase()
{
返回Base::createBase();
}
这两个函数的“全名”如下所示:

template<class T> T* Derived::Base<Something>::createBase<T>();

static_cast(d)->createBase();
或者,再加上杰帕莱切克的回答

static_cast<Base<T> >(this)->createBase(); 
static_cast(this)->createBase();

应该工作,我认为是类型安全;也就是说,如果
这个
不是继承自

这是正确的编译器行为吗?我认为这会导致模棱两可和编译错误。我对将模板与继承混合使用感到有些奇怪。无论如何,如果要避免冗长的语法,请使用
typedef
static_cast<Base<Something> >(d)->createBase();
static_cast<Base<T> >(this)->createBase();