Advertisement

Sorry but another OOP question

Started by December 21, 2000 09:38 AM
1 comment, last by FuMo 24 years ago
I''m having problems with graphics engine I''m working on. I''m currently trying to learn OOP for C++ and I thought that it would be ideal for my graphics engine because I want to implement an number of draw modes. ie Software, OpenGL, etc; So I have got as follows at the moment:
  
class Engine
{
public:
  Engine() {};
  ~Engine() {};
  virtual setup() = 0;
  virtual draw() = 0;
};

class OpenGLEngine: public Engine
{
  OpenGLEngine() {// int stuff}

  ~OpenGLEngine () {//destroy stuff}

  setup();
  draw();  
};
[/source]

Then in the main program I have got...
[source]
Engine *pGfxEngine;

switch(MODE)
{
case RENDER_SOFTWARE:
  ...
  break;
case RENDER_OPENGL:
  pGfxEngine = new OpenGLEngine();
  break;
}
  
Anyway when I want to destroy the OpenGLEngine it will not call the OpenGLEngine destructor only the Engine desctructor. Thanks for any help.
+---------------------------------------------+| "Hard work never killed anyone but I figure || why take the chance?" Ronald Regan |+---------------------------------------------+
Make your destructor virtual.

Edited by - Jrz on December 21, 2000 11:15:05 AM
Advertisement
Jrz is right.

Because pGfxEngine is initially an object of Engine. You are using polymorphism to access your derived class. And so when you destroy pGfxEngine it is calling Engine::~Engine() not OpenGlEngine::~OpenGlEngine(). If it was virtual, it would then call ~OpenGLEngine() before ~Engine().

Kevin =)

-----------------------------
kevin@mayday-anime.com
http://www.dainteractiveonline.com
-----------------------------kevin@mayday-anime.comhttp://www.mayday-anime.com

This topic is closed to new replies.

Advertisement