inheritance within templates in C++
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.
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.
o ok thanks, forgot about that. is there a way to do it tho? cause like you said, it would give clearer errors.
template<class T> class Foo{ BOOST_STATIC_ASSERT((boost::is_base_of<Base, T>::value)); ...};
edit - added missing ()
[Edited by - Fruny on June 13, 2006 9:44:32 AM]
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
-Etyrn
Quote:Original post by Frunytemplate<class T> class Foo{ BOOST_STATIC_ASSERT(boost::is_base_of<Base, T>::value); ...};
Well, with an extra set of parenthesis around the macro argument (otherwise it'd be interpretted as two arguments instead of one).
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]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement