Re: Types of constructors



Responding to Sathyaish...

Caveat: any answer here depends on the specific language implementation. Constructors tend to be very language-dependent.

What is a private constructor, and why would a class have one? What are
the other kinds of constructors besides:

(1) public constructors; and
(2) parameterized constructors

A private constructor is one that is visible only to the class or members of the class. The most common use is when objects are instantiated through a special static Class method. For example, one might need to support a Find(id) capability and one doesn't want to clutter the constructor itself with the infrastructure for managing an ordered list of instances.


A second use occurs when one member of the class needs to instantiate another member of a class in a special manner. [FWIW, I am not a fan of this because the context of instantiation usually involves business rules and policies that are unique to the problem context. Since objects abstract intrinsic responsibilities of underlying problem space entities, it is hard to rationalize why an object should have that sort of knowledge about its own context of use.]

Another use is when a constructor is parameterized and the parameterization causes the instance to be created with different attributes initialized by default. One can have the parameterized constructor dispatch to the right private constructor. [FWIW, I am not a fan of this sort of parameterization because it implies that in some circumstances the object is created with uninitialized attributes. That strikes me as a marvelous opportunity for foot-shooting. IOW, I think all attributes should be initialized by default.]


And I understand that they are not mutually exclusive of one another. The above classification assimilates my knowledge of having used constructors in both the above manners.

Public and private constructors are mutually exclusive but parameterized constructors can be either.



************* There is nothing wrong with me that could not be cured by a capful of Drano.

H. S. Lahman
hsl@xxxxxxxxxxxxxxxxx
Pathfinder Solutions  -- Put MDA to Work
http://www.pathfindermda.com
blog: http://pathfinderpeople.blogs.com/hslahman
(888)OOA-PATH



.



Relevant Pages

  • Re: Shared classes or modules?
    ... initialization code that is executed the very first time you use any member ... I could have the Sub New above open the log file the first time I went ... Other posts say that a module have no default constructor but it can ... >> Also you should make the constructor private, to prevent instantiating ...
    (microsoft.public.dotnet.languages.vb)
  • Re: C++ 101 dumb question
    ... Its action is to copy the data members of the class, ... If one of those members is a pointer, ... have it's own copy constructor would the default copy constructor call ... as a copy of the class being returned is placed on the stack for the ...
    (microsoft.public.vc.language)
  • Re: Error using ==> class
    ... The argv in there is obviously a holdover. ... varargin because varargin apparently does not actually allow nargin == ... the change, no changes to code are made, and the constructor (as seen ... All instances of an object must have the same member names in the same ...
    (comp.soft-sys.matlab)
  • Re: ECMAScript standard:the code(s) for figure on page 3 ?
    ... You need to assign that object to the public prototype property of the ... assigned a reference to the empty anonymous function on the right hand ... Instance have property from constructor. ... member create from scope of constructor function. ...
    (comp.lang.javascript)
  • Re: composition/aggregation: would like to use constructor body rather than initializer list
    ... "constructor chaining" takes in C++; this type of mechanism is essential ... and ubiquitous (whether you're dealing with pointers or not) across all OO ... >the constructor or get some non-trivial input info, before building member ... The contemporary wisdom in C++ as it applies to initialization lists is ...
    (alt.comp.lang.learn.c-cpp)