After creating a instance of a class, can we invoke the constructor explicitly? For example
class A{
A(int a)
{
}
}
A instance;
instance.A(2);
Just to summarize, the three ways to specify the explicit constructor are via
A instance(2); // does A instance = 2; ever work?
A *instance = new A(2); //never sure about & versus * here, myself
new (&instance) A(2);
and flavors of those. The idea goal is to arrange that at no time is an object constructed that is not in a proper initialized state, and constructors are designed to assure that. (This means that methods don't have to check on whether some .init(...) method has been successfully called or not.)
This strikes me as the more-functional way to go about this, especially for classes that are parts of frameworks and reused in libraries. If that is what you are interested in, work toward having all constructors, including any default one, deliver a fully-working instance.
Exception Cases: There are things you might not have in the constructor operation if it is possible for them to fail, unless it is appropriate to throw an exception from the constructor. And some folks like having "blank" instances that are propogated using subsequent methods and even exposed-to-initialization members. It is interesting to explore ways to mitigate such situations and have robust instances that don't have bad states that need to be protected against in method implementations and in usage.
PS: In some complex cases, it may be useful to have an initialized instance (reference) be delivered as the result of a function or of a method on a "factory" class, so that the intermediate, under-setup instance is never seen outside of the encapsulating factory class instance or function. That gives us,
+4. A *instance = MakeAnA(2);
+5. A *instance = InterestingClass.A(2);