How does a c++ line like this Foo t3 = Foo::construct(true);
work when the default constructor is private? My assumption (which is obviously incorrect), is that the default constructor is called, followed my the assignment operator. That assumption must be incorrect because the default constructor is private, and cannot be called.
A concrete example:
class Foo {
private:
Foo() {}
bool bar;
public:
Foo(bool t): bar(t) {}
static Foo construct(bool t) {
Foo temp; //calling private constructor;
temp.bar = t;
return temp;
}
}
And a test method for instantiating this class looks like:
int main() {
//Foo t1; //Not allowed, compile error, Foo() is private
Foo t2(true); //Constructor, valid use
Foo t3 = Foo::construct(true); //It works! Why?
return 0;
}
What is really happening behind the scenes when t3
is instantiated?