Question

My main class is :

class Data{
public:
    Data(std::string name):name_(name){};
    virtual ~Data(){};

protected:
    std::string name_;
};

Since I'd like to allow any kind of data, I use a template to create my inherited class :

template<typename Type>
class GenericData : public Data{
 public:
    GenericData(std::string name, Type t);

private:
    Type t_;
};

template<typename Type>
GenericData<Type>::GenericData(std::string name, Type t):
    Data(name),
    t_(t)
{}

The third class is a collection of (Generic)Data :

class Input{
public:
    Input();
    ~Input();

private:
    std::vector<Data*> d_;
};

Input::Input(){
    d_.push_back(new GenericData("double",3.2);
    d_.push_back(new GenericData("int",2);
}

Input::~Input(){
    for(unsigned int i(0);i<d_.size();i++){
    delete d_[i];
    d_[i] = NULL;
    }
}

The thing that I don't know how to do is to get the t_ value from from the Input class. I'd like to have a templata<typename Type> Type Input::get(unsigned int i) method that returns t_. I guess that I should create a virtual method in Data and another method in GenericData but I can't make it work.

Was it helpful?

Solution

It's pretty straightforward:

#include <string>
#include <vector>
#include <iostream>

class Data{
public:
    Data(std::string name):name_(name){};
    virtual ~Data(){};

protected:
    std::string name_;
};

template<typename Type>
class GenericData : public Data{
 public:
    GenericData(std::string name, Type t);

    Type Get() { return t_; }
private:
    Type t_;
};

template<typename Type>
GenericData<Type>::GenericData(std::string name, Type t):
    Data(name),
    t_(t)
{}

class Input{
public:
    Input() {}
    ~Input();

    void Add(Data*v) {d_.push_back(v);}
    template<typename T>
    T Get(int i) {
        return dynamic_cast< GenericData<T> *>(d_[i])->Get();
    }
private:
    std::vector<Data*> d_;
};

Input::~Input(){
    for(unsigned int i(0);i<d_.size();i++){
    delete d_[i];
    d_[i] = NULL;
    }
}

int main() {
        Input inp;
        inp.Add(new GenericData<int>("1", 1));
        inp.Add(new GenericData<double>("2", 2.0));
        std::cout << "1: " << inp.Get<int>(0) << "; 2: " << inp.Get<double>(1) << std::endl;
        return 0;
}

OTHER TIPS

That's quite problematic. First of all, you have to provide some way to get it from Data, for example:

(inside GenericData)

public:
    T GetValue()
    {
        // Consider naming t_ more informative
        return t_;
    }

Secondly, you can create a generic method for getting data in Input:

(inside Input)

public:
    template <typename T>
    GenericData<T> * GetData(int index)
    {
        return dynamic_cast<GenericData<T> *>(_d[index]);
    }

or, simpler:

(inside Input)

public:
    template <typename T>
    T GetValue(int index)
    {
        return (dynamic_cast<GenericData<T> *>(_d[index]))->GetValue();
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top