Jump to content
  • Advertisement

Archived

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

GekkoCube

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 way

CObject *obj = new Derived;

// second way

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

Share this post


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

That should work if inheritance is involved

-----
Losec
-----

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

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!