what is the virtual/abstract interface good for?
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.
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.
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.
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.
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.
If you want to use pure virtual functions (abstract functions), its also a way of forcing programmers to provide functionality.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement