在类构造函数中使用模板化类型 [英] Use a templated type in a class constructor

查看:100
本文介绍了在类构造函数中使用模板化类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何做到这一点。



I am wondering how to accomplish this.

template <class T>
class dynamic
{
    friend myclass;
    T dyn_data;
    dynamic() : dyn_data(0) { }
    dynamic(T Data) : dyn_data(Data) { }
}

class myclass
{
private:
    void * m_data;
public:
    myclass() : m_data(nullptr) { }
    ~myclass() { if (m_data != nullptr) delete m_data; }
    template <typename Type> myclass(Type data) { SetData<Type>(data); }
    template <typename Type> void SetData(Type data)
    {
        m_data = new dynamic<Type>(data);
    }
    template <typename Type> void GetData() const
    {
        dynamic<Type> * data;
        data = static_cast <dynamic<Type>*> (m_data);
        return (Type) data->dyn_data;
    }
}

int main()
{
    //This code works as designed
    myclass data1;
    data1.SetData<int>(12);
    int x = data1.GetData<int>();

    //This code also works but defaults the type to a double
    myclass data2(123.4);
    float y = data2.GetData<float>();

    //But intellisense flags this for errors
    myclass<float> data3(234.5);  //class 'myclass' cannot have a template argument list
    float z = data3.GetData<float>(); // multiple errors on this line

    //This is also flagged as an error
    myclass * data4 = new myclass<float>(345.6);
}





简而言之,我试图将模板化类型传递给类构造函数,以便内部模板化类可以构造。

通过使用默认构造函数创建类并通过模板函数填充数据是可能的。

Internet搜索template和constructor返回除了实现模板类的链接之外什么都没有。



我想我的问题是,是否可以在类构造函数中传递模板参数?

我已经尝试了有错误的线路的多种配置但我找不到有效的。



如果是的话,我做错了什么。

如果没有,那么这是一个有趣的想法。



In a nutshell, I am trying to pass templated types into a class constructor so that an internal templated class can be constructed.
It was possible by creating the class with the default constructor and populating the data through a template function.
Internet searches for "template" and "constructor" return nothing but links to implementing template classes.

I guess my question is, is it possible to pass template arguments in a class constructor?
I've tried multiple configurations of the line that has the error but I cannot find one that works.

If it is, what am I doing wrong.
If not, well it's an interesting idea.

推荐答案

您无法传递模板参数,但编译器可以自动为您找出类型。在某些情况下,你必须明确地对ctor参数进行类型转换以指定确切的类型。



你去了,这是一个有效的bugfixed例子:

You can not pass template arguments but the compiler can automatically find out the types for you. You have to explicitly typecast the ctor arguments in some cases to specify the exact types.

There you go, here is a working bugfixed example:
class base_data
{
public:
    // This virtual "method" will make sure that the base class has a vtable and in turn, RTTI typeinfo.
    // We need the virtual destructor here anyway because we destruct the data using a pointer to the base class...
    // Deleting a base class pointer when the base class doesn't have a virtual destructor is always a BUG.
    virtual ~base_data() {}
};

template <class T>
class dynamic : public base_data
{
    friend class myclass;
    T dyn_data;
    dynamic() : dyn_data(0) { }
    dynamic(T Data) : dyn_data(Data) { }
};

class myclass
{
private:
    base_data* m_data;
public:
    ~myclass()
    {
        delete m_data;
    }
    myclass() : m_data(nullptr) {}

    template <typename Type>
    myclass(Type data)
    {
        m_data = new dynamic<Type>(data);
    }

    template <typename Type>
    void SetData(Type data)
    {
        delete m_data;
        m_data = new dynamic<Type>(data);
    }

    // This seems to be a useless method in this form...
    template <typename Type>
    Type GetData() const
    {
        dynamic<Type> * data;
        // slow, but it doesn't crash at least...
        data = dynamic_cast<dynamic<Type>*>(m_data);
        return data ? data->dyn_data : Type();
    }
};

void test()
{
    //This code works as designed
    myclass data1;
    data1.SetData<int>(12);
    // the compiler finds out that Type is int
    data1.SetData(12);
    int x = data1.GetData<int>();

    // the compiler finds out that Type is float
    myclass data2(123.4f);
    float y = data2.GetData<float>();

    myclass data3(234.5f);
    float z = data3.GetData<float>();

    // the compiler finds out that Type is double
    myclass * data4 = new myclass(345.6);
    delete data4;
}


这篇关于在类构造函数中使用模板化类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆