Sign in to follow this  
ehmdjii

how to best instanciate a new class

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
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
You could pass a struct pointer or a reference.


struct POD
{
par1 a;
par2 b;
par3 c;
};

class FOOBAR
{
POD m_POD;
public:

FOOBAR(POD* pPOD)
{
assert(pPOD); // validate parameter
memcpy((void*)&m_POD,pPOD,sizeof(POD)); // Cast is evil!
};

};



Yes, I know the C-style cast is evil.
This saves stack bashing, but costs you a level of indirection within your class.
Alternatively, you can unwrap the passed pointer in the constructor, and not have the indirection:



class FOOBAR
{
par1 m_a;
par2 m_b;
par3 m_c;

public:

FOOBAR(POD* pPOD)
{
assert(pPOD);
m_a = pPOD->a;
m_b = pPOD->b;
m_c = pPOD->c;
};

};



This works well if you're creating a lot of instances with similar parameters, or parameters that don't change much - you simply tweak your single instance of the argument struct as you go along.

Share this post


Link to post
Share on other sites
Why not:


struct POD
{
par1 a;
par2 b;
par3 c;
};

class FOOBAR
{
POD m_POD;
public:

FOOBAR(POD* pPOD)
{
assert(pPOD); // validate parameter
m_POD = *pPOD;
};

};

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this