0

i have a class shown below:

template <class TValue>
    class ICData
    {
    private :
TValue value;
    public:
inline ICData()
{
};
TValue get_value();
void set_value(TValue data);
    };

    template <class TValue>
    TValue ICData<TValue>::get_value()
    {
return value;
    }

     template <class TValue>
     void ICData<TValue>::set_value(TValue _value)
     {
value=_value;
     }

i know how to make a pointer array by:

    ICData <int> *ICArray[10];
    ICArray[0]=new ICData<int>();
    ICArray[1]=new ICData<int>();

but is there any way to make a pointer array using template ? like below:

            template <class T>// iknow this code is WRONG
    ICData <T> *ICArray[10];
    ICArray[0]=new ICData<int>();
    ICArray[1]=new ICData<float>();

thanks in advance.

6
  • You mean a template declaration? Templates are for type redefinitions, but you need to supply the template value at some point. Commented Dec 27, 2011 at 8:50
  • i know that i should supply it some where : ICArray[0]=new ICData<int>(); Commented Dec 27, 2011 at 8:55
  • i wonder how do you intend to use ICData <T> *ICArray[10];? Commented Dec 27, 2011 at 8:55
  • code updated to show my request more clear . Commented Dec 27, 2011 at 8:56
  • @Donotalo look at the difference : ICArray[0]=new ICData<int>(); ICArray[1]=new ICData<float>(); Commented Dec 27, 2011 at 8:57

2 Answers 2

1

If you wanted to store a pointer that could point to either a ICData<int> or a ICData<float> then those types would have to have a common base class and you could use that common base as the type to point to.

As it stands the type ICData<int> and ICData<float> are not related so other than using void * there is no solution to what you want to do that wouldn't involve some ugly casts.

Sign up to request clarification or add additional context in comments.

4 Comments

void *ICArray[10]; ICArray[0]=new ICData<int>(ln); ICArray[1]=new ICData<float>(ln2); this code works but how can i access ICArray[0].get_Value()?
@mefmef: You have to cast it back to the correct type first. e.g. static_cast< ICData<int>& >(ICArray[0]).get_Value()
:Error 15 error C2440: 'static_cast' : cannot convert from 'void *' to 'ICData<TValue> &' c:\documents and settings\h.farazi\my documents\visual studio 2010\projects\icarraytest\main.cpp 22 1 ICArrayTest
@mefmef: Sorry, my mistake, I meant: static_cast< ICData<int>* >(ICArray[0])->get_Value(). I'm not sure why you are getting TValue in the error message, though.
1

You cannot use this approach to store both ICData<int> and ICData<float> in the same ICArray.

It could be made to work, if you derive ICData<T> from a non-templatized ICDataBase, and use ICDataBase* ICArray[10]. However, the static type will be lost so most likely (you would want to use the stored value at some point, won't you?) this will lead to a lot of casting.

Another option is to use boost::any.

However, usually there is a better approach without using boost::any (although it is justified in some cases). So I would suggest to take a step back and rethink your solution.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.