PDA

View Full Version : would this be a good idea?


EscapeCharacter
11-22-2002, 03:18 AM
i have a couple classes that have several overloaded constructors and was wondering if it would be a good idea to have the ones with one or more arguments to call the default constructor from within all the other ones before they change whatever specific thiing it is they change. i ask because the default constructor would set *all* the possible attributes while the overloaded ones would take one or two arguments and change only one or two things.

ps. if this doesnt make sense forgive me im really really tired

sans-hubris
11-22-2002, 09:22 AM
It's fine, ^].

That would work out great, actually. Code reuse is a Good Thing(TM). It helps cut down on errors.

jemfinch
11-22-2002, 01:31 PM
The other reason you should call default constructor and then change some values is that if you set the values prior to calling the default constructor, those values might get clobbered by the default constructor.

Jeremy

stuka
11-22-2002, 01:31 PM
Sure sounds like a plan to me....

EscapeCharacter
11-22-2002, 05:39 PM
ok cool sounds like a plan, im suprised yall understood what i was try to get across :)

ok but now it gets a little more complicated. say i have three constructors, the default one that takes two argument and one that takes three arguments. they are as follow:


Constructor::Constructor(){
...
//set all default values
}

Constructor::Constructor(A, B){
...
a = A;
b = B;
}

Constructor::Constructor(A, B, C){
...
a = A;
b = B;
c = C;
}

now give that would it be more appropriate to just reuse these and cut it down to this:

Constructor::Constructor(){
...
//set all default values
}

Constructor::Constructor(A, B){
...
Constructor();
a = A;
b = B;
}

Constructor::Constructor(A, B, C){
...
Constructor(A, B);
c = C;
}

i know this is probably obvious but i want to know for sure to save myself from having to change things back later

Kentaro
11-22-2002, 05:53 PM
The way I've seen it done is to create a constructor with more parameters and call that one from the constructors with less parameters. So Constructor(A, B) calls Constructor(A, B, C) and passes a value of your choosing for C... something like that.

sicarius
11-22-2002, 09:50 PM
why not just have constructor(A, B, C) have default values in the prototype. That way callers wouldn't even have to(**) put in the ones they didn't want.

for instance:
constructor(A = 5, B = NULL, C = NULL)


This would set the values to their given defaults if no other value was given by the user. A call to the constructor could be:
constructor(7);
where 7 is the user's value for A. B and C would both be set to NULL.

** Of course the problem is if the user wants to set C and not A and B, they would have to enter values for A and B anyway.

EscapeCharacter
11-22-2002, 09:52 PM
Originally posted by Kentaro
The way I've seen it done is to create a constructor with more parameters and call that one from the constructors with less parameters. So Constructor(A, B) calls Constructor(A, B, C) and passes a value of your choosing for C... something like that.

if i did it that way C wouldnt be know and would be set to some default value in Constructor(A, B) unless im not understanding you right that would be kinda the opposite of what i was thinking of doing