Jump to content
  • Advertisement
Sign in to follow this  
CaossTec

Pointer to a template class

This topic is 4976 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I create a template class like this
template <typename T>
class cA
{
    public:
        T valor;

        void DoSomething();
};
and then I want to create a pointer to an object of that class having any type just to call DoSomething() e.g.
  cA* pBuffer;  // cA can be of any type so not allowed cA<float>* pBuffer

 // .....///
   pBuffer =  new cA<float>;
   pBuffer->DoSomething();
but that fail when compiling. And even worst if I do std::vector< cA* > apBuffers; apBuffers.push_back( new cA<float> ); Any idea or hint? Pd. If I do cA<typename>* pBuffer instead of cA* pBuffer, the compiler (VS2003) guess I'm refering to cA<int>* pBuffer;

Share this post


Link to post
Share on other sites
Advertisement
Templates are still statically typed. cA< float > is a completely different type to cA< int >. There is also no such thing as a cA object. cA is only a template and only instantiations of that template can exist. So you can do cA< float > * object = new cA< float >(), but cA< float > * object = new cA< int >() is just as nonsensical to the compiler as Dog * dog = new Motorcycle().

If you need to store a number of objects from a related group you can either use inheritance or some form of union/type-safe multi-type object, for example boost::variant or boost::any.

Enigma

Share this post


Link to post
Share on other sites
Quote:
Original post by CaossTec
I create a template class like this


template <typename T>
class cA
{
public:
T valor;

void DoSomething();
};


and then I want to create a pointer to an object of that class having any type just to call DoSomething()

e.g.


cA* pBuffer; // cA can be of any type so not allowed cA<float>* pBuffer

// .....///
pBuffer = new cA<float>;
pBuffer->DoSomething();


but that fail when compiling. And even worst if I do

std::vector< cA* > apBuffers;
apBuffers.push_back( new cA<float> );

Any idea or hint?

Pd. If I do cA<typename>* pBuffer instead of cA* pBuffer, the compiler (VS2003) guess I'm refering to cA<int>* pBuffer;


You can't do it. Until you make a cA class of a specific type, there is no cA class. The classes you make with cA are also unrelated to each other.

You might want to make a base class for your pointers:


class cABase
{
public:
virtual void doSomething = 0;
};

template <typename T> class cA : public cABase
class cA
{
public:
T valor;

void DoSomething();
};

caBase *buffer;

buffer = new cA<float>;

buffer->doSomething();//Runs cA<float>::doSomething();


You won't have access to cA<>::valor variables through pointers and referenes to cABase. The only stuff you can put in cABase is stuff that will be true for all classes generated using the cA template.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!