Jump to content
  • Advertisement
Sign in to follow this  
ehmdjii

how to best instanciate a new class

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

hello, say i want to create a new instance of a class which has quite a lot of members. is it better to provide them all as parameters to the constructor or to assign them all as public variables. example1:
//constructor
I::I(type var1, type var2, type var3, type var4, ...)
{
x1 = var1;
x2 = var2;
x3 = var3;
x4 = var4;
}


var * I = new I(var1, var2, var3, var4, ...);


; example2:
var * I = new I();

I->x1 = var1;
I->x2 = var2;
I->x3 = var3;
I->x4 = var4;
...


the problem is, i really have a lot of variables and the constructor looks kinda weird with all these parameters.

Share this post


Link to post
Share on other sites
Advertisement
I'd limit the constructor args to only necessary items, initialize all the others in the constructor initializer lists to sensible values, and allow the caller to modify them through modifier functions, rather than making them public. Using modifier functions you can validate the input for correctness before setting it and assert or whatever. Public variables if the class is a POD type.

Share this post


Link to post
Share on other sites
Unless it is one of those odd homeworks, just use important arguments for constructors, for example the size of an array the class would use, or if the class seriously requires the path of a file that's a good one too. For the rest use default, dummy values

Share this post


Link to post
Share on other sites
It Depends.

On the number of parameters, whether there are sensible values for them, whether they have any logical groupings, whether some can be calculated from others (some may be explicitly intended as caches of precalculated values; others may turn out to be unnecessary upon further reflection), etc.

You may consider the Named (Constructor) Parameter Idiom.

Share this post


Link to post
Share on other sites
Although its kind of an immature guide, when the parameter list extends past the 80th column, there are probably too many. Same goes for functions and the like. What you should probably confine to the constructor is just the data that makes an object relatively unique. For example, a class containing data about a ball, you'd need it's radius, color, location on the screen. But for this same ball, you probably wouldn't include metadata like which direction its rolling, or how much of a bounce it has to it.

Share this post


Link to post
Share on other sites
I recently started trying to do the stuff Zahlman links too, and it works really well for most lengthy constructors that often have default parameters.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!