Jump to content
  • Advertisement
Sign in to follow this  
AcidInjury

limiting templates to specific classes? (C++)

This topic is 4884 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 heres the situation. I have a class ResManager, another class Node, and a third class DXNode that inherits from Node. ResManager is designed to work with Node objects, and it dynamically creates and stores these objects. The problem I am having is this. I want to make ResManager capable of working with either Node or DXNode objects. But due to the dynamic memory allocation in some of the methods, I have to choose one type. My first thought was to template ResManager so it could create either, but it feels wrong having a template class that is only designed to work with one base type and all its derived types. Yes, trying to use ResManager<Foo> would likely generate errors because Foo PROBABLY doesnt implement the required methods, but it feels wrong. Is this just a limit of templates? Is templating the only way to go? Can I limit a template class to only accept specific classes? The situation feels wrong to me. Btw, the reason for the split is that I want to keep all DirectX specific information out of the Node class since its unrelated to the functionality of the class. Thanks, Will

Share this post


Link to post
Share on other sites
Advertisement
You can also make both classes inherit from an abstract base class, and make your manager be unaware of their existence (that it, it is only aware of the existence of the base class).

Add in an object factory to create the correct kind of object for you without the manager having to know which, and you're all set.

EDIT:

And since you're asking, yes, the unability to check for a signature is both an advantage and a disadvantage of templates.

It's an advantage because you can instantiate a list of any kind of object even if the "sort" method requires an "operator<" to be implemented, because if you don't use the sort method the compiler will not complain and the code will be correct.

The disadvantage is that quite often you intend for the template arguments to have certain properties, only it is perfectly possible to instantiate a template with an incorrect argument, only to discover later that you can't use it because it was incorrect.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
No template necessary. Just implement Node as an interface class (pure virtual member functions that define the interface), and your resource manager becomes a node manager.

The DXNode object has no choice but to implement the interface (since it is derived from Node).

Share this post


Link to post
Share on other sites
Quote:
Original post by AcidInjury
Ok heres the situation.

I have a class ResManager, another class Node, and a third class DXNode that inherits from Node.

ResManager is designed to work with Node objects, and it dynamically creates and stores these objects.

The problem I am having is this. I want to make ResManager capable of working with either Node or DXNode objects. But due to the dynamic memory allocation in some of the methods, I have to choose one type. My first thought was to template ResManager so it could create either, but it feels wrong having a template class that is only designed to work with one base type and all its derived types. Yes, trying to use ResManager<Foo> would likely generate errors because Foo PROBABLY doesnt implement the required methods, but it feels wrong. Is this just a limit of templates?


It's a limit of C++ templates. That said, one can program in such a way that incorrect parameters allways (I think) cause errors, and also more informative ones - one of those ways would be the BCCL (Boost Concept Check Library). I suggest you check out The rest of the boost library as well. Oh, and a link to the actual Homepage instead of just documentation.

Quote:
Is templating the only way to go? Can I limit a template class to only accept specific classes? The situation feels wrong to me.


That's your inner advocate for "Design by Contract Programing" (I think that's the right term) speaking. I usually pay mine no heed, but I'll admit there are definately benifits, and the lack of such enforcements has bitten me in the ass more than once (usually with the boost library, it being the main library I use). For me, the costs outweigh the benifits, however.

Others have touched as much as I would on the alternative to templatization (having the appropriate interface in Node) so I won't touch on that.

Oh yeah, and a quick incomplete class just to give you brain candy to munch on (in regards to the "dynamic memory == 1 type" bit):

class resource_manager //What can I say, I hate ThisTypeOfTyping :P
{
std::map< std::string , resource_node * > nodes; //And I'm very verbose.
public:
template < typename node_t > //Err, no demo for BCCL since I don't use it, sorry.
void load( const std::string & id , const std::string & filename )
{
assert( !nodes[id] );
node_t * node = new node_t( filename );
nodes[ id ] = node;
}
template < typename node_t >
node_t * lookup( const std::string & id ) //should be const, but map::op[] isn't, and I'm lazy, and this is an example, so be quiet :P
{
node_t * node = dynamic_cast< node_t * >( nodes[id] );
assert( node );
return node;
}
~resource_manager( void )
{
using namespace std;
for_each( nodes.begin() , nodes.end() , compose1( delete_f() , select2nd< map<int, double>::value_type>() ) );
}
};



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!