Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

How does a factory constructor invoke a constructor?


Asked by Katelyn Guerrero on Dec 01, 2021 FAQ



A factory constructor invokes another constructor. Since a factory constructor does not directly create a new instance, it cannot use a constructor initializer list. A normal constructor always returns a new instance of the class. A factory constructor is permitted to return an existing instance, an instance of a derived class, or null.
Keeping this in consideration,
A factory method can return an already created object, unlike a constructor, which always creates a new instance. Create a factory method. Place a call to the current constructor in it. Replace all constructor calls with calls to the factory method. Declare the constructor private.
Moreover, The constructor gets invoked right after the object is initialized and is not required to call the constructor explicitly. Once the constructor is invoked, it assigns memory to the resources. A constructor is a special class member function of a class that initializes objects i.e. class instance).
Furthermore,
The constructor is like a function with/without parameter but it doesn’t have a return type. For example, this is Customer class with constructor that has the same name: Now you can create new object using a constructor. If we don’t define any constructor, the default constructor below will be created.
In fact,
When implemented in a derived class, invokes the constructor reflected by this ConstructorInfo with the specified arguments, under the constraints of the specified Binder. Invokes the constructor reflected by the instance that has the specified parameters, providing default values for the parameters not commonly used.