Sign in to follow this  

inheritance within templates in C++

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

ok I know java has a way to specify that you want the generic type to be a subclass or a super class of a specific class (<T extends Object>, <? super SomeCLass>). Is there any way to do this in C++? Im trying to make a particle engine and I want a base particle class that contains the minimum data needed. However you can make a particle system out of anything that inherits from particle. I know I could use virtual functions for this but Id rather keep the particles as small as possible. 4 bytes per particle is a lot if your using 1000s of particles.

Share this post


Link to post
Share on other sites
In a nutshell, you can just skip requiring an inheritance relationship as part of the template declaration. With latent typing in the C++ template mechanism, as long as the instantiated type supports the operations you perform in the template implementation (which can include casting to the base type) then the template will compile. If the type does not support the operations performed by the template implementation then the code will fail to compile. Admittedly this gives a less clear error message than the Java generics.

Share this post


Link to post
Share on other sites
It's part of the boost library. A library that supposedly fills many missing voids in standard C++. It's pretty useful, but I myself have never really had a need for it. :p


-Etyrn

Share this post


Link to post
Share on other sites
Quote:
Original post by Polymorphic OOP
Well, with an extra set of parenthesis around the macro argument (otherwise it'd be interpretted as two arguments instead of one).


[razz] [smile]

Share this post


Link to post
Share on other sites

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