具有模板成员变量的C ++类 [英] C++ class with template member variable

查看:200
本文介绍了具有模板成员变量的C ++类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解决由一个包含几个参数的对象(称为图)组成的编程问题。每个参数(Parameter类)可以是以下几种类型之一:int,double,complex,string-仅举几例。

I am trying to solve a programming problem that consists of an object (call it Diagram), that contains several parameters. Each parameter (the Parameter class) can be one of several types: int, double, complex, string - to name a few.

所以我的第一个直觉是定义我的图类具有模板参数的向量,如下所示。

So my first instinct was to define my Diagram class as having a vector of template parameters, which would look like this.

class Diagram
{
private:
    std::vector<Parameter<T> > v;
};

这无法编译,我知道原因。因此,根据此页面上的建议,如何声明数据成员是类中任何类型的对象,我将代码修改为:

This doesn't compile, and I understand why. So, based on the recommendations on this page How to declare data members that are objects of any type in a class, I modified my code to look like:

class ParameterBase
{
public:
    virtual void setValue() = 0;
    virtual ~ParameterBase() { }
};


template <typename T>
class Parameter : public ParameterBase
{
public:
    void setValue() // I want this to be 
                    // void setValue(const T & val)
    {
        // I want this to be 
        // value = val;
    }

private:
    T value;
};

class Diagram
{
public:
    std::vector<ParameterBase *> v;
    int type;
};

我很难弄清楚如何使用适当的模板化参数调用setValue函数。在ParameterBase抽象基类中不能有模板化参数。

I'm having trouble figuring out how to call the setValue function with an appropriate templated parameter. It is not possible to have a templated parameter in the ParameterBase abstract base class. Any help is greatly appreciated.

P.S。我没有使用boost :: any的灵活性。

P.S. I don't have the flexibility to use boost::any.

推荐答案

您距离很近。我添加了一些内容,因为它们很方便

You got very close. I added a few bits because they're handy

class ParameterBase
{
public:
    virtual ~ParameterBase() {}
    template<class T> const T& get() const; //to be implimented after Parameter
    template<class T, class U> void setValue(const U& rhs); //to be implimented after Parameter
};

template <typename T>
class Parameter : public ParameterBase
{
public:
    Parameter(const T& rhs) :value(rhs) {}
    const T& get() const {return value;}
    void setValue(const T& rhs) {value=rhs;}    
private:
    T value;
};

//Here's the trick: dynamic_cast rather than virtual
template<class T> const T& ParameterBase::get() const
{ return dynamic_cast<const Parameter<T>&>(*this).get(); }
template<class T, class U> void ParameterBase::setValue(const U& rhs)
{ return dynamic_cast<Parameter<T>&>(*this).setValue(rhs); }

class Diagram
{
public:
    std::vector<ParameterBase*> v;
    int type;
};

图可以执行以下操作:

Parameter<std::string> p1("Hello");
v.push_back(&p1);
std::cout << v[0]->get<std::string>(); //read the string
v[0]->set<std::string>("BANANA"); //set the string to something else
v[0]->get<int>(); //throws a std::bad_cast exception

看起来您的意图是存储资源拥有的指针在向量中。如果是这样,请小心使 Diagram 具有正确的析构函数,并使其不可复制且不可复制。

It looks like your intent is to store resource-owning pointers in the vector. If so, be careful to make Diagram have the correct destructor, and make it non-copy-constructable, and non-copy-assignable.

这篇关于具有模板成员变量的C ++类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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