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

Base class constructors/destructors.

Started by GekkoCube Feb 2, 2003 at 4:19 AM 6 replies 250+ views
Original Post
GekkoCube
GekkoCube
I have this base class called CObject. it is basically for my game engine, so the pure virtual functions I have is Render(...), and thats about it. First of all, I noticed that any derived class from CObject like this:
  CObject *obj = new Derived;  
...doesn''t call it''s destructor when i delete it, even though the constructor is called. So the question(s): 1) how do i get constructors and destructors to work in derived classes?? 2) what other methods would be useful in a game-based Base class like CObject?
VolkerG
VolkerG
Try a virtual destructor. And yes, think about what you are doing.
civguy
civguy
1) put a virtual destructor in CObject-class
2) is CObject really necessary? Does it give any benefits to you? If not, get rid of CObject and don''t inherit just for the sake of inheritation. It''s highly likely that your CObject is useless, from what you''ve described.
Seriema
Seriema
Civguy, 2)

Of course that is useful! You can have a list of CObject* that actually contains cubes, triangles, pyramids, or whatever.

"No lies of sugar can sweeten the sournes of reality"

}+TITANIUM+{ A.K.A. DXnewbie[onMIRC]
[ ThumbView: Adds thumbnail support for DDS, PCX, TGA and 16 other imagetypes for Windows XP Explorer. ] [ Chocolate peanuts: Brazilian recipe for home made c
GekkoCube
GekkoCube
It may not be necessary to have CObject.
but it will make things cleaner and easier to read since i can have a list of these CObjects and call its Render() function straight thru the list.

I tried a virtual constructor and virtual destructor (both were pure virtual). I had problems compiling the pure virtual destructor though. I''ll try again.

Also, are the following equivocal?

  
// one way

CObject *obj = new Derived;

// second way

Derived *derived = new Derived;
CObject *obj = derived;

Losec
Losec
Yeah try a virtual destructor:
virtual ~CObject(); (i think)

That should work if inheritance is involved

-----
Losec
-----
civguy
civguy
quote:
Original post by GekkoCube
I tried a virtual constructor and virtual destructor (both were pure virtual). I had problems compiling the pure virtual destructor though. I''ll try again.
Constructor can''t be virtual. If destructor is pure virtual, it still needs to be defined (it''s a special virtual function). Like this:

  
struct A {
virtual ~A() = 0;
};

A::~A() {}

But if Render() is already pure virtual, you don''t need to make the destructor pure virtual in order to make the class abstract.
quote:
Also, are the following equivocal?

Yes.

Topic Locked

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

Sign in to reply to this topic.