My main question is: can I put the generic
type before a class
definition?
Like, can I do something like this:
generic class Classname (ParameterType parameter)
{
cout << "Hello world";
}
My main question is: can I put the generic
type before a class
definition?
Like, can I do something like this:
generic class Classname (ParameterType parameter)
{
cout << "Hello world";
}
Templates are the way to do generics in C++. You can write it like this:
template<class ItemType> class ClassName
{
public:
ClassName(const ItemType& newdata) : data(newdata) {}
private:
ItemType data;
};
Later on in main:
ClassName<int> data1(1);
ClassName<char> data2('A');