Is it possible to instantiate one class object that calls two constructors?
I have a one-parameter constructor and a two-parameter constructor. I would like the call the first one, and then the second one as soon as I make the object. Is this possible in c++?
And then somewhere in another file, you call both of them at the same time? As far as I can see, I don't think that's possible, for what I think is a similar reason to having two functions in two different libraries with identical names. It'd be ambiguous. The compiler wouldn't know which one to do processes with.
The only way the compiler would know is dependent on the number of parameters you gave to your constructor, and if you're using VS or Codeblocks (I've never used MinGW or Turbo so I don't know if those do it), there's the fancy little list that pops up bolding whatever parameter you're on if you have two same-name functions with different parameter lists.
I am guessing a bit as to what you are trying to achieve here. For what you have in your second ctor, make that a private function instead and have the first ctor call that function.
If you still need both ctor's, then consider factoring out the code that is common to both ctor's and putting it into a private function.
struct Number
{
Number(int n) : i(n) {} //If user provides number, store it
Number() : Number(42) {} //Else we delegate initialization to other constructor
int i;
}
int main()
{
Number n;
assert(n.i == 42);
}