Sign in to follow this  

Getting the template type from a pointer to it

This topic is 1269 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 have a class template which inherits from a base class and the template class have a member variable that can be of any type( just like in stl continers).

 

 

class declartion:

class fooBase
{
public:
    int x;
    int y;

};

template <class T>
class fooTemplate : public fooBase
{
public:
    T data;
};

now after I  instantize the template class I save a pointer to it in a vector that holds pointers of the base class type.

 

all works fine till I wanted to get the pointer to my template class back and found out that I can't cast it back unless when I call the function to get the pointer back I tell it to what type of the template to cast it.

int main ()
{
	std::vector<fooBase*> foos;

 	fooTemplate<int>* fooTemp = new fooTemplate<int>;
	foos.push_back(fooTemp);
	
	getTemplate<int>(foos[0]);//works fine
	getTemplate(foos[0]); //compile error for not telling it the template type

	delete fooTemp;

	return 0;
}

my question is if there is a way to save type of the template so I don't need to tell the function the type of the template ?

or some way to catch if I by mistake I cast it to the wrong template type ?

 

also could I be just thinking of this all thing in the wrong way ?

Share this post


Link to post
Share on other sites

Make fooBase a virtual class (add an empty virtual destructor), then you can at least dynamic_cast<fooTemplate<type>*>() to make sure that an object has type fooTemplate<type>. The calling function will still need to know the template type, but at least this adds type safety.

 

There isn't really any way to do better than that in C++, because there is no reflection support. If you want to perform an action on an object like that, the best way (that doesn't use down casting) would be to have a pure virtual function in fooBase that is implemented for each fooTemplate<type>.

Edited by Aressera

Share this post


Link to post
Share on other sites

You can't get the type back statically. The vector does not know the subclass type. That's the point of this kind of design.

 

Usually when you have an inheritance relationship like that, you should avoid casting to the child class, and instead use virtual methods.

 

It is possible to go back to the child type statically, using RTTI, but that has a runtime cost. I doubt that's what you want.

 

What problem are you trying to solve with this?

Share this post


Link to post
Share on other sites

The problem I am trying to solve is that I building a GUI and I have  a listbox and it stores items in it , each item has a text that gets renders for it and a Data that it holds(which can be of any type).

In order to get the current items selected in it i need to call function that is in the listbox that returns them to me.

The listbox itself inherits from control which is the base class for all the other controls.

Share this post


Link to post
Share on other sites

In that case you have a fixed universe of data types. In stead of using a template that can take any type, you should probably create a variant class that only handles that fixed list of types.

 

You don't know the type in the list at compile time, so you can't use anything that would deduce the static type for you. If you use inheritance, you need to rely on virtual functions to do something depending on the type of the data. In rare cases, you might need dynamic_cast.

Share this post


Link to post
Share on other sites

Its probably worth pointing out that templates are not really relevant to this, although it seems to be causing some confusion.

 

If B and C derive from A and I have an A*, we do not know whether A *a is a B or a C.

 

If T<> derives from A, then bear in mind T<int> and T<float> are as distinct and unrelated types as B and C are in the example above. So the fact we know that A *a points to a T<something> is not useful information. We know no more about what *a points to than we did in the first example, even if we know that a points to a T<something>.

 

Can we find out? Yes, of course. dynamic_cast and RTTI have been discussed. My point is that the involvement of templates makes this no different to the simpler way of looking at it.

Share this post


Link to post
Share on other sites

This topic is 1269 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this