Reputation: 1342
In C++, is it possible to have a constructor in such a way that, say if I have an object with 5 parameters: double P1
, double P2
, double P3
, double P4
, boolean P5
, and I could have my constructor works in such a way that if I say:
MyObject (double param1, double param2, true)
, then param1 is assigned to P1 and param2 is assigned to P2, P3 and P4 are assigned to 0 and P5 is assigned to true
And if I do:
MyObject (double param1, double param2, false)
, then param1 is assigned to P3 and param2 is assigned to P4, P1 and P2 are assigned to 0 and P5 is assigned to false?
I.e., the constructor used is based on the value of the last parameter passed in?
Upvotes: 2
Views: 106
Reputation: 13940
Here's yet another look at an option. Instead of changing or providing two, because that's not what you want to do, and instead of having the expanded code in your constructor try this:
MyObject(double param1, double param2, bool param3) {
P5 = param3;
if (P5)
defaultConstructor(param1, param2, 0.0, 0.0);
else
defaultConstructor(0.0, 0.0, param1, param2);
}
void defaultConstructor(double param1, double param2, double param3, double param4) {
P1 = param1;
P2 = param2;
P3 = param3;
P4 = param4;
}
};
This to me is straight forward and a bit cleaner - opinion of course. Sorry for pointing out the obvious in the comment ;)
Upvotes: 1
Reputation: 490623
If the true
and false
are always constants (i.e., you're always going to pass either true
or false
, not some Boolean variable), then you could use templates and specialization to do this. Frankly, I have a hard time advising it.
Seems to me the most straightfoward way to do the job is just use an if
statement in the ctor:
MyObject(double P1, double P2, bool b) {
if (b) {
P1_ = P1;
P2_ = P2;
P3_ = P4_ = 0.0;
P5 = true;
}
else {
P3_ = P1;
P4_ = P2;
P1_ = P2_ = 0.0;
P5 = false;
}
}
Offhand, however, I think I'd step back and think harder about what you're doing. Passing Boolean as a parameter is generally a bad idea, because it does a poor job of conveying meaning -- even at best, the meaning of MyObject(1, 2, true);
vs. MyObject(2, 3, false);
isn't obvious. In your case, it looks even less meaningful than usual, to the point that you'd need to read the ctor to have any clue of what either one does.
Summary: yes, it's possible to this -- but I really doubt it's a good idea.
Upvotes: 1
Reputation: 89
Just have an if statement in your constructor
MyObject(double par1, double par2, bool par3)
{
this->p5 = par3;
if(par3)
{
this->p1 = par1;
this->p2 = par2
this->p3 = 0;
this->p4 = 0;
}
else
{
this->p1 = 0;
this->p2 = 0;
this->p3 = par1;
this->p4 = par2;
}
}
Upvotes: 1
Reputation: 4444
Yes, you can have multiple constructors with different signatures, and one constructor that behaves differently based upon the passed arguments. Though you might not want to get too crazy, as people using your class would have a hard time if the constructors behave strangely.
Upvotes: 1
Reputation: 36092
You can have multiple constructors
C(double P1, double P2, double P3, double P4)
: m_P1(P1), m_P2(P2), m_P3(0),m_P4(0), m_P5(0) { ; }
C(double P1, double P2)
: m_P3(P1), m_P4(P2) {;}
...
In general with functions it is better not to have a bool to determine behavior because it makes the function more difficult to read, instead it is better to split up the functionality in two so that the functionality is distinct from one another.
PrintAllHtml(bool b)
says less than
PrintHtmlHead()
PrintHtmlBody()
and may be easier to modify later.
Upvotes: 2