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:
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);
}
template <typename Type>
Type GetData() const
{
dynamic<Type> * data;
data = dynamic_cast<dynamic<Type>*>(m_data);
return data ? data->dyn_data : Type();
}
};
void test()
{
myclass data1;
data1.SetData<int>(12);
data1.SetData(12);
int x = data1.GetData<int>();
myclass data2(123.4f);
float y = data2.GetData<float>();
myclass data3(234.5f);
float z = data3.GetData<float>();
myclass * data4 = new myclass(345.6);
delete data4;
}