Archived

This topic is now archived and is closed to further replies.

Base class constructors/destructors.

This topic is 5743 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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?

Share on other sites
hmm. Here''s some advice. Learn C++. Learn OO. Then realize what you did wrong. The reference section is full of stuff to help you learn.

Please. I''m not trying to put you down. Just that if you can''t udnerstand what you''re doing wrong or how to fix it you really need to properly learn C++. Consider this a wake up call.

Share on other sites
Try a virtual destructor. And yes, think about what you are doing.

Share on other sites
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.

Share on other sites
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]

Share on other sites
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 wayCObject *obj = new Derived;// second wayDerived *derived = new Derived;CObject *obj     = derived;

Share on other sites
Yeah try a virtual destructor:
virtual ~CObject(); (i think)

That should work if inheritance is involved

-----
Losec
-----

Share on other sites
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.

1. 1
Rutin
46
2. 2
3. 3
4. 4
5. 5
JoeJ
19

• 11
• 15
• 9
• 10
• 13
• Forum Statistics

• Total Topics
633004
• Total Posts
3009837
• Who's Online (See full list)

There are no registered users currently online

×

Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!