English 中文(简体)
模板模板类继承
原标题:Template class inheritance

我对以下代码有问题(这是一个非常简单的例子,

#include <iostream>

using namespace std;

template<class T> class CBase
{
    public:
        template <class T2> CBase(const T2 &x) : _var(x) {;}
        template <class T2> CBase (const CBase<T2> &x) {_var = x.var();}
        ~CBase() {;}
        T var() const {return _var;}
    protected:
        T _var;
};

template<class T> class CDerived : public CBase<T>
{
    public:
        template <class T2> CDerived(const T2 &x) : CBase<T>(x) {;}
        template <class T2> CDerived (const CBase<T2> &x) : CBase<T>(x) {;}
        ~CDerived() {;}
};

int main()
{
    CBase<double> bd(3);
    CBase<int> bi(bd); // <- No problem
    CDerived<double> dd1(3);
    CDerived<double> dd2(dd1);
    CDerived<int> di(dd1); // <- The problem is here
    return 0;
}

错误如下:

error: cannot convert  const CDerived<double>  to  int  in initialization

如何解决这个问题? (优先修改基类而不是衍生类,如果可能,也不使用虚拟性)

非常感谢你

EDIT : If I replace the concerned line with : CDerived<int> di(CBase<int>(CBase<double>(dd1))); it works but it is not very practical...

看来解决的方法是:

template <class T2> CDerived(const CDerived<T2> &x) : CBase<T>(static_cast<const CBase<T2>&>(x)) {;}
最佳回答
CDerived<int> di(dd1); // <- The problem is here

这引用了 Didd < CDerived 的第一个构建者, 因此 > >D2 被推断为 CDerived< ; brother> >, 被推断为 CDerived< breen> , 被传递给接受 Dcode > 的基底类构建者(这是这种类型的参数 d>dd1 类模板的价值)。 因此,错误不能转换为 CDerived< bruny> 无法转换为 < bcode > < ccode > < ccode > < ccode > < ccode > a/code > 。

如下所示:

CDerived<int> di(dd1); // <- The problem is here
          ^       ^
          |       |
          |       this helps compiler to deduce T2 as double
          |
          this is T of the CDerived as well as of CBase

如果您想要使您的代码工作, 那么做到这一点:

  1. First derive publicly instead of privately.
  2. Add another constructor taking CDerived<T2> as parameter.

所以,你需要这样做:

template<class T> class CDerived : public CBase<T>  //derived publicly
{
    public:
        template <class T2> CDerived(const T2 &x) : CBase<T>(x) {;}

        //add this constructor
        template <class T2> CDerived(const CDerived<T2> &x) : CBase<T>(x.var()) {;}

        template <class T2> CDerived (const CBase<T2> &x) : CBase<T>(x) {;}
        ~CDerived() {;}
};

它现在应该能够工作了:在线演示

问题回答

尝试在基类中另建一个构造器,该构造器将取自一个通用对象,并使用动态铸造来指定值。

template <class T2> CBase (const Object &x) : _var() {
    try {
        const CBase<T2> &x_casted = dynamic_cast<const CBase<T2> &> (x);
        _var = x_casted.var();
    }
    catch {
        std::cerr << "Object not of type CBase" << std::endl; 
    }
}

注意: 这可能会被视为不怎么风格。 动态铸造在运行期间比使用 < code> vitual 和超载更昂贵, 所以考虑重新设定代码 。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?