Jump to content
  • Advertisement
Sign in to follow this  
tulio

C++ templates

This topic is 4264 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

Hi everybody, I have a template class and some classes that inherits it and define the type of the template arguments. Some thing like: template <class T> class Parent { }; ... class Child : Parent <RealType> { }; class Child2 : Parent <RealType2> { }; ... In another class a want to have a vector of Parent to hold different types of Child. I can't find the right syntax, is this possible? class Container { public: void addParent (Parent* t); } Parent can be any of its child.

Share this post


Link to post
Share on other sites
Advertisement
Templates are a compile-time inheritance, not a runtime inheritance. There is no relationship between Parent<RealType> and Parent<RealType2>, so you can't have a container that can hold objects of both types.

That's why you can't find the syntax to do what you;re trying to do: it's not possible.

If you need to implement an is_a relationship, you will need to derive template<typename T> Parent from some common non-templated base class. You Child and Child2 would then have an is_a relationship with the non-template ParentBase class.

Share this post


Link to post
Share on other sites
If RealType != RealType2, then it's impossible without any more information (of course it isn't completely impossible since you could just keep void* pointers, but you'd need to determine their type at some point). Parent<T> and Parent<U> are different template classes if T and U are different, therefore Child and Child2 inherits from two different classes. What exactly are you trying to achieve? Does Parent have some virtual functions you want to call? Do they depend on T? If they don't you can just make Parent inherit from another class which just have the virtual functions. If you want us to really give a good answer you need to provide more information.

Share this post


Link to post
Share on other sites
I could very well be wrong here but I don't think this can be done, it would be possible to do something like...

class Container
{
public:
void addParent (Parent<RealType>* t);
};

The option I can think of is have the Parent<class T> derive from some common interface and use a pointer of that type...

e.g.

class BaseType {
};

template <class T>
class Parent : BaseType {
}


class Container
{
public:
void addParent (BaseType* t);
};

But like I said I'm not too sure about this, thats just how I understand it and I could well be wrong.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
have a look at boost any:
http://www.boost.org/doc/html/any.html
http://www.boost.org/boost/any.hpp

this may give you the idea of how to accomplish what you want.

Share this post


Link to post
Share on other sites


What exactly are you trying to achieve?
As you've said it's impossible to do that. I searched for something similar and didn't find so this post was my last hope.


Does Parent have some virtual functions you want to call?
Yes.
Do they depend on T?
Yes, :S

I think I will try another approach.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
have a look at boost any:
http://www.boost.org/doc/html/any.html
http://www.boost.org/boost/any.hpp

this may give you the idea of how to accomplish what you want.


The problem here is that he still needs to know the type to call any_cast, assuming that Child and Child2 is the only children then it's possible, but it will be an ugly hack which there sure is much better alternatives to.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by CTar
Quote:
Original post by Anonymous Poster
have a look at boost any:
http://www.boost.org/doc/html/any.html
http://www.boost.org/boost/any.hpp

this may give you the idea of how to accomplish what you want.


The problem here is that he still needs to know the type to call any_cast, assuming that Child and Child2 is the only children then it's possible, but it will be an ugly hack which there sure is much better alternatives to.


Why would you still need to know the type, if you have a pointer to the container which inherits from the interface then call the type func. Without knowing all the details I can not say for sure if any or an alternative to any is the correct methd.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Anonymous Poster
Quote:
Original post by CTar
Quote:
Original post by Anonymous Poster
have a look at boost any:
http://www.boost.org/doc/html/any.html
http://www.boost.org/boost/any.hpp

this may give you the idea of how to accomplish what you want.


The problem here is that he still needs to know the type to call any_cast, assuming that Child and Child2 is the only children then it's possible, but it will be an ugly hack which there sure is much better alternatives to.


Why would you still need to know the type, if you have a pointer to the container which inherits from the interface then call the type func. Without knowing all the details I can not say for sure if any or an alternative to any is the correct methd.

[correction]if you have a pointer to the interface then call the type func[/correction]

Share this post


Link to post
Share on other sites

class Parent
{
public:
virtual void interface_func_1()=0;
virtual void interface_func_2()=0;
};

template<class T>
class TemplatedParent<class T> : public Parent
{
public:
void interface_func_1();
void interface_func_2();
};

//if the only difference between Child and Child2 is the template type:
typedef TemplatedParent<RealType> Child;
typedef TemplatedParent<RealType2> Child2;

//if Child and Child2 have other differences
class Child : public TemplatedParent<RealType>
{
...
};

class Child2 : public TemplatedParent<RealType2>
{
...
};


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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!