Skip to main content
GameDev.net gamedev.net
🔒 Locked

interface classes templates

Started by EvilTachikoma Oct 28, 2002 at 6:42 AM 4 replies 200+ views
Original Post
EvilTachikoma
EvilTachikoma
Okey, I am wondering is there a way to use template classes as interfaces or do templates complitely destroy polymorphism while deriving new classes from the interface class with a specified type. Consider this
    
template <class T>
class IBaseA
{
// pure virtual stuff here

}

class A1 : IBaseA <type1>
{
// methods for doing stuff with type1 here

}

class A2 : IBaseB <type2>
{
// methods for doing stuff with type2 here

}


/* Is there a way to write this fuction so that it can deal with 
derived classes with a specified type ? */

void DealWithIBaseA(IBaseA &a)
{
  a.DoStuff()
}
    
[edited by - EvilTachikoma on October 28, 2002 7:44:10 AM]
SabreMan
SabreMan
quote:
Original post by EvilTachikoma
/* Is there a way to write this fuction so that it can deal with
derived classes with a specified type ? */

void DealWithIBaseA(IBaseA &a)
{
a.DoStuff()
}

Do you mean like this:


  
template<typename T>
void DealWithIBaseA(const T& t);

template<>
void DealWithIBaseA<A1>(const A1& a)
{
a.DoStuff();
}

template<>
void DealWithIBaseA<A2>(const A2& a)
{
a.DoOtherStuff();
}


Or do you mean something different?
EvilTachikoma
EvilTachikoma
Well, I''ll guess that shall do. Thanks I am really new to templates and exploring how to implement them in the most usefull ways. Writing an extra function for each new IBaseA derived class is a bit tedious though, isn''t there a syntax to avoid that, if not than I could live with it .
SabreMan
SabreMan
quote:
Original post by EvilTachikoma
Writing an extra function for each new IBaseA derived class is a bit tedious though, isn''t there a syntax to avoid that

It depends on really what you want to achieve and why. Your example doesn''t make it clear that you even need to use templates - I could say that you simply override DoStuff() for each derived class. Of course, that still requires you to write an implementation for each override. The benefit of both approaches is in having a uniform interface, so that you don''t need to discover any more about the entity you are acting on than the minimum you need for a particular context. IOW, it can aid encapsulation.
EvilTachikoma
EvilTachikoma
quote:
Original post by SabreMan
It depends on really what you want to achieve and why. Your example doesn't make it clear that you even need to use templates - I could say that you simply override DoStuff() for each derived class. Of course, that still requires you to write an implementation for each override. The benefit of both approaches is in having a uniform interface, so that you don't need to discover any more about the entity you are acting on than the minimum you need for a particular context. IOW, it can aid encapsulation.


Edit: quote tags were messed up

Yes, I thought about that too (not using templates that is, but I decided be bold and use them to see where that approach would take me). The thing I am trying achive is making a generic interface for initialization and deinitialization of different devices (Video, Sound, Joysticks ect.). When initializing something you have some requirements that you want to be met by the device... and ofcource then there is the reality of the situation.

The specific requirements, what the system actually has and the acceptable possibilites are to be stored in the type (different for each device ofcource). But there are alot of similar things you will do with any given device (thus I though a generic interface would be a neat thing to have). You initialise using the approprite type defined to hold information about that device, get information about how the initialization went and eventually deinitialize.

Now if we have an interfce, you could just give the class of a specific device to a function and it will try to initialize and report the situation after initialization. All the ugly system specific stuff will be encapsuled in the class and the main program should remain relatively unchanged when adding support for new devices (3d goggles, Toeasters equipt with gamecontrollers ... whatever is needed ).

Here comes the main point ...

The need for templates comes in when trying to build the possibility of different datatypes that hold information about different devices directly into the interface class and the function that handles that interface.

[edited by - EvilTachikoma on October 28, 2002 9:44:59 AM]
SabreMan
SabreMan
quote:
Original post by EvilTachikoma
Now if we have an interfce, you could just give the class of a All the ugly system specific stuff will be encapsuled in the class and the main program should remain relatively unchanged when adding support for new devices (3d goggles, Toeasters equipt with gamecontrollers ... whatever is needed ).

If you use compile-time polymorphism to implement this scheme, you will have to recompile the application when adding support for new devices. Is that really what you want to do? If not, templates are the wrong solution.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.