Jump to content
  • Advertisement

Archived

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

Turtlebread

Constructors and Init functions

This topic is 6167 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

Ok, say I have a CObject class. This class has several pure virtual functions, including an initialize function. Now, when intializing CObject''s members in a class derived from CObject, I have two choices. I could create a constructor for CObject and have it intializes the members that way. Or, I could have the intialization function defined in the derived class do it. It seems to me that the way that makes the most sense is using a constructor. Now, if I use a constructor for the base class, wouldn''t it make sense to use one for the derived class? So is the intialization function unneccessary? Or should I have there be two options, constructor or init function? Any opinions(especially those with examples in game programming) on anything I''ve stated above? -Chris

Share this post


Link to post
Share on other sites
Advertisement
You *could* use both methods, which makes sense because then you could create an empty CObject, and call Init() on it to initialize the data anytime you want, or create a CObject with parameters to use the constructor, which just calls the Init() function anyway. For example:

    
class CObject
{
private:
int sampledata;

public:
CObject();
CObject(int data);
void Init(int data);
void Clear(void);
};

CObject::CObject()
{
Clear();
}

CObject::CObject(int data)
{
Init(data);
}

void CObject::Init(int data)
{
sampledata = data;
}

void CObject::Clear(void)
{
sampledata = 0;
}



This is obviously oversimplified, but this is how I understood it from an article in my Game Programming Gems book. You gain the ability to create the object separate from initializing the object, which is good OOP practice, and allows you to reuse the dynamically allocated memory over and over again for different things. Like:


  
CObject * pobject = new CObject;
pobject->Init(5);
// use pobject for a little while;

pobject->Clear();
pobject->Init(3);
// use the new value in pobject for a little while




At least, that's how I understand it anyway. Well, and this is a concrete class, not a virtual base, but the idea is the same.

Paradigm Shift 2000

Edited by - Paradigm Shift 2000 on September 25, 2001 7:49:15 PM

Share this post


Link to post
Share on other sites
Ok, I see what you''re saying. Its led me to another question.

My CObject class has a bitmap structure(used only to load the bitmap, then eliminated). I could declare it private in the CObject class and then have it loaded via a constructor, or a protected function. Or, I could declare the bitmap structure private. I guess what I''m asking is, How much functionality should a base class have? Should it be purely abstract? Or should it have some "general" functions, like loading a bitmap to a DDraw surface?

-Chris

Share this post


Link to post
Share on other sites
Still, if you forced the initialization of any derived object through its constructor and didn''t supply it with an Init() function, you would have to deallocate and reallocate new instances everytime you wanted to load it with new data, which is expensive and wasteful when you could re-use the same dynamically allocated memory.

Paradigm Shift 2000

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!