Jump to content
  • Advertisement
Sign in to follow this  
derek7

what is the virtual/abstract interface good for?

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

it just convenient to code ? class IA{virtual do() ...}; class CB: IA{virtual do() ...}; class CC: IA{virtual do() ...}; class CD: IA{virtual do() ...}; So IA * p[3]; CB p1;p[0]=p1; CC p2;p[1]=p2; CS p3;p[2]=p3; for (int i=0;i<3;i++) p->do(); it is all good for abstract ? if there are some other good ,please give simple sample.

Share this post


Link to post
Share on other sites
Advertisement
It's for abstraction, basically.

An example would be a 3D renderer. You can define the base interface as IRenderer, and then provide several different implementations stored in DLLs. For example, you could have a Direct3D implementation as well as an OpenGL one.

Another example is game entities. By defining an abstract class IGameEntity, and deriving all subsequent game entity types (player, monster, bullet, lightning-gun, etc.) from it, you can easily store all game entities regardless of type in the same container (e.g. std::list), via polymorphism. By doing this, you could perform various operations on them without knowing their types, because they all conform to a uniform interface.

Share this post


Link to post
Share on other sites
It allows you to work with a generic type without having to know about a specific implementation. Take the aforementioned rendering class. All of my code can work with an IRenderer interface, without needing to know whether this IRenderer is actually an OpenGLRenderer, a Direct3DRenderer, a SoftwareRenderer, or whatever.

Share this post


Link to post
Share on other sites
Right, you have a list of Entity pointers, and can store any subclass of entity in the entity pointer list.

If you want to use pure virtual functions (abstract functions), its also a way of forcing programmers to provide functionality.

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!