d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Storing Template Class In Vector
Add Reply New Topic New Poll
Member
Posts: 1,358
Joined: Dec 30 2012
Gold: 0.10
Oct 16 2013 11:54pm
Playing around and trying to learn templates.
Soo my question is, how could I go about storing a template class in a pre declared vector?

Here is a small amount of code I've type up thus far

Configuration.cpp:

Code
template<class T>
CSettingValue<T>::CSettingValue(T& t_Obj, T t_Value, unsigned short type)
{
t_Obj = t_Value;
p_Obj = t_Obj;
u_Type = type;
}


Configuration.h:

Code
enum
{
BOOLEAN = 1,
STRING,
USHORT
};

template<class T>
class CSettingValue
{
private:
T* p_Obj;
T t_Value;
unsigned short u_Type;

public:
CSettingValue(T&, T, unsigned short);
};


What I would like to do is have a vector that will be used for managing various different data types of CSettingValue objects

i.e

Code
std::vector<CSettingValue*> settings;

settings.push_back(new CSettingValue<std::string>(v_StrDataMember, "value", STRING));
settings.push_back(new CSettingValue<unsigned short>(v_UshortDataMember, 111, USHORT));
settings.push_back(new CSettingValue<bool>(v_BoolDataMember, true, BOOLEAN));

for(std::vector<CSettingValue*>::iterator it = settings.begin(); it != settings.end(); it++)
{
delete *it;
}


Something like that.. I suck with this template / polymorphism stuff atm. Suggestions?

This post was edited by SelfTaught on Oct 17 2013 12:01am
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll