Jump to content
  • Advertisement
Sign in to follow this  
CDProp

How much should be done in a constructor?

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

Is there a limit? I mean, beyond initialization of the member variables. Suppose, for instance, I had a GameWindow class which handles most of the Win32 stuff, and in its constructor, I registered, created, and showed the window...would that be too much? I don't exactly have a reason for asking this except that it "feels" wrong. I usually use the constructor to initialize default values and then I use a separate Init() method to handle the setting up of the object, if it indeed requires more setup than just straight initialization. But if there's no need to separate it into two functions, I won't do it anymore. : ) Edit: You can't throw exceptions in constructors, right? So I guess that might be one reason, I dunno.

Share this post


Link to post
Share on other sites
Advertisement
In most situations, the completion of the constructor should leave the object in a stable, known, usable. As such, in most situations you should do as much work as is neccessary to place the object in that state. You may optionally do more, at your discretion.

This means, in fact, that errors in the work a constructor performs should throw exceptions -- you cannot return values from a constructor so the only other option is to early-out of the constructor and leave the object in a 'zombie' state, where it exists but is not technically alive. This is often regarded as bad design, and only in situations of extremely performance sensitivity are constructors excused from leaving objects in a fully-usable state.

Quote:

Suppose, for instance, I had a GameWindow class which handles most of the Win32 stuff, and in its constructor, I registered, created, and showed the window...would that be too much?

I don't exactly have a reason for asking this except that it "feels" wrong. I usually use the constructor to initialize default values and then I use a separate Init() method to handle the setting up of the object, if it indeed requires more setup than just straight initialization. But if there's no need to separate it into two functions, I won't do it anymore. : )

There is usually no need to separate construction and "Init()" methods. It's usually a bad idea. However, in this specific example, showing the window by default is not something I would expect either the constructor or the Init() method to do, neccessarily. However, registering and creating the window are certainly appropriate actions for a Window object constructor. The only reason I'd shy away from showing the window by default were to allow the user of the object to perform additional manipulation of the window prior to displaying it so that the user of the program did not see a visual artifact as some property of the window were changed, which can sometimes happen depending on implementation detail.

Quote:

Edit: You can't throw exceptions in constructors, right? So I guess that might be one reason, I dunno.

It is destructors you "can't" throw from (actually, should not, you can technically write and compile the code to do so but it is a bad idea because you can cause you application to be terminated if you throw during a stack unwind caused by another exception).

Share this post


Link to post
Share on other sites
From a logical perspective, if I type "new Window()" anywhere in my code, I expect there to then be a window. Not a set of data that I can then manipulate to create a window.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!