Jump to content
  • Advertisement
Sign in to follow this  
tion80

About initializing..

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

Hi guys. I got some questions. Well, Altough I'm newbee as a programmer, I've been seeing some codes to adjust in to my own 3d engine. and I am confused about how I initialize an OBJECT.. First time, based on my knowledge in oop, I thought I'd be supposed to use a constructor to initialize. but, these days, I've found it out that I can also put some default values for parameters in parmeter list in the constructor.. it also look cool !! However, it seems like common for experienced programmer to use the initialize function rather than the constructor... Why is this? and what's the case that we have to use a constructor? or default value? can anyone give me some idea? or good example? thanks~!

Share this post


Link to post
Share on other sites
Advertisement
In the constructor you can use MIL to init most variables, the constructor doesn't have a return value however. Some objects may fail to initialize so u need to know about them (return false, etc), others may require something else setup before the initialization (other objects).

I usually make an 'Init' (coupled with a 'Shutdown') function but in some cases it isn't needed.

Share this post


Link to post
Share on other sites
I can't speak for everyone but here is how I do it in some cases.

Say my Direct3D class for example, my constructor consists of this:

Direct3D::Direct3D()
{
Init();
}

So when the constructor is called it fires the Init function.

Why do I do this? Because I can call the Init function any time I want to, it does not rely on constructing an object.

An example would be when I want to comletely re-initialize but actually keep the object alive. If I had the Init code in the constructor instead of it's own function, I wouldn't be able to do that.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I often use an init/shutdown method pair but I do attribute initialization in the constructor as well. You could place whatever in the constructor and have it throw any errors, nicely caught within a try/catch block.

Share this post


Link to post
Share on other sites
I'm not sure that using an Initialise function is the Right Thing to do. If an object can't be created properly then it would make sense to throw an exception from the constructor.

If you create an object and then always immediately call Initialise() then you may as well put that into the constructor.

If you create an object and don't use it until you've called Initialise(), which may be later on, you may as well not create it until then and put the code in the constructor instead.

If Initialise() doesn't depend upon any other objects then you might as well put it in the constructor.

If Initialise() depends on the existence of other objects, create them first and use the constructor.

If the object can be valid even without Initialise being called, then Initialise doesn't really initialise the object - it should be called something else like ConnectToServer for example. The object can behave reasonably even if it hasn't been connected to the server.

Share this post


Link to post
Share on other sites
wow. it sounds like quite useful.. but I can't see what the sentence below means..

"
If you create an object and don't use it until you've called Initialise(), which may be later on, you may as well not create it until then and put the code in the constructor instead.

"

what is meant by 'the code' .. the code for initialization? so you mean, don't make init function and don't create an object until I actually need it.. right?

however, I've seen that there would be some situations like we need to make a 'blank' object and put some parameters ... is this wrong programming style?

help ~! ;>

Share this post


Link to post
Share on other sites
I include an initialize function in most of my classes because sometimes I want to create an object but not initialize it until later. This is useful when things need to be initialized in a specific order, but I can't gaurantee that they will be constructed in that order. For example, my class that interfaces with D3D depends on the assumption that the window has already been created, but I usually make it global, so initialization would fail if it was done at construction time. This is not the best example, since that particular object probably doesn't need to be global (it's usually accessed through a singleton anyways), but it shows the order thing and there are cases when it really is easier to just have a separate initialize function (especially when objects are members of classes). Plus this method has the added bonus of allowing me to reinitiallize most objects at will. A similar effect could be achieved by using pointers and then dynamically allocating objects when one would otherwise have initialized them, but because of how easy it is to make mistakes with pointers I prefer not to use them in this way unless I have to. Having an initialize function also makes creating arrays of objects easier: sometimes I want to declare an array of objects where each object will have different input values for initialization. With an initialize function I can just create the array and then have a loop where I call each initialize function; I have no idea how to achieve the same effect with constructors.

Share this post


Link to post
Share on other sites
Error during initialization are quite common. For instance your data files are not found, because they are in the wrong path.
Or your display cannot be opened because it is in use by another application.

You as the developer have to decide, what should happen in such cases. If initialization of parts fail that are required to run your application (e.g. graphics device), it should be cought up as early as possible. You may use exceptions in such cases that are thrown in the constructor.

Exceptions also allow to return immediately to the point where it can be handled, e.g. a loadData() function that calls loadSound(), loadGraphics(), ... fails because one of the subfunctions failed. So let the exception pass to the point where loadData() was called from.

However exceptions add additional code size and slow down the code a bit (but manual checks for return values do that also).


Initialization functions on the other hand are usefull if your objects must be constructed beforehand (e.g. as global objects) and the initialization parameters are retreived later on (e.g. read from a file).

Another use of initialization functions would be the case of reinitializing an object.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!