Hello. I have checked around the web and the site, and decided to create a post here.
Default class constructors are a bit of a sore spot in my realm of understanding, as the majority of samples use content with which I am unfamiliar (such as templates).
I read some articles about using templates to create workarounds, but I felt that was getting too complex, since what I want is essentially just cutting a few lines of code.
Class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
class FunTimes {
public:
void SetDefaults();
void SuperSpecialAwesomeFunction1();
void SuperSpecialAwesomeFunction2();
FunTimes(int, int, int); // main constructor
FunTimes(int, int); // overloads
FunTimes(int);
Funtimes();
private:
int some_int;
int another_int;
int yet_another_int;
char come_char;
bool some_bool;
};
Definitions
1 2 3 4 5 6 7 8 9 10 11 12 13
void FunTimes::SetDefaults() { // used to set values to defaults
int some_int = 1;
int another_int; = 2;
int yet_another_int = 3;
}
void FunTimes::SuperSpecialAwesomeFunction1() {...}
void FunTimes::SuperSpecialAwesomeFunction2() {...}
FunTimes::FunTimes(int, int, int) {...} // main constructor
FunTimes::FunTimes(int, int) {SetDefaults();} // overloads
FunTimes::FunTimes(int) {SetDefaults();}
FunTimes::Funtimes() {SetDefaults();}
Above is a rough example of what I have. When an object of type FunTimes is created, it can take a range of 0-3 arguments. However, if fewer than 3 are given, the arguments will be disregarded and default values will be set. I CAN write out the overload for each potential case, but this would be tiresome and I would love to learn something today.
Is there an effective way to make a sweeping(default) constructor for all cases that do not match another constructor?
class Fun
{
Fun(int = 0, int = 1, int = 2);
};
Fun::Fun(int a, int b, int c):
some_int(a), some_other(b), third_int(c) //Use constructor list
{
}
Note how the default parameters are only in the function declaration and not the definition.
Also, this way if you want to set parameter "b" you also need to set "a".
EDIT: Also, there is no point to a "SetDefaults" function unless:
A. You need a return value, and in this case it is pointless to stick the SetDefaults in the constructor.
or
B. You need to reset the defaults sometime during the objects lifetime.
You also forgot the return values on your non-constructor functions.
Nope, it's exactly the other way around. If you don't specify a parameter the compiler (Which is why the defaults need to go into the declaration) will stick the default value in for you. It's just syntactic sugar.
If you were to write FunTimes(2, 56, 1001) these values would override the default values. If you were however to write FunTimes(1), then the first value would be overridden, and the other two would use the default.
Very interesting. However, my issue still stands. If fewer than three inputs are given, I need to set all three to default. As I see it, by using default parameters I can then check each parameter for validity (the defaults obviously pass), but unless I somehow count the number of inputs provided in creation of the object, I will never know how many were provided.
For example,
Lets say I have defaults of 1, 2 and 3 for Fun::Fun(int a, int b, int c)
If I have Fun Bob(1, 2, 3) then it will be identical to the default.
If I have Fun Bill(7, 8, 9) then these values will be used.
If I have Fun Bo(1, 2) then it will be identical to the default, yet only two arguments were used in creation of the object. However, this would be a case where I would want to implement the default values.
Finally, if I have Fun Juniper(92, 4) then this would be another case where I would want to use the default values, despite 92 and 4 being perfectly valid.
Something like va_start and its companion functions don't really suit my situation, as I do not know how many arguments I will be given.
I can always just write the overload for each potential case if this turns out to not have an optimal solution.
Sorry, I missed the part about using the default whenever less than three parameters are given. If this is the case, why are you even writing constructors for cases with less parameters? Just have one constructor that takes three ints, and one default constructor that sets the values to their default value. I'm sorry if I don't quite understand, it just seems odd to me.
What is the context for this class? What does it need to accomplish?
EDIT: If you really need it to accept 0-3 parameters you could do something like this:
1 2
Fun(int a, int b, int c):int1(a),int2(b),int3(c){} //Parameters given
Fun(int a = 0, int b = 0):int1(default1), int2(default2),int3(default3){}
This way if you say something like Fun(2,3) it won't even use those values.