Re: Calling a constructor from another



Responding to Sathyaish...

How does a constructor of one class call another of the same class?

A constructor is essentially just a class-level responsibility, so it is just another method call (except there is no implied 'this' argument).


When would this mechanism make sense or be required?

The most obvious need is for inheritance. Since the object being instantiated is a composite of all the properties in all classes in the direct line of ascent in the subclassing tree, the usual technique is to provide a constructor for each class to handle that class' properties and invoke them in a nested fashion.


PS: Two instances I saw are:

(a) While using the Singleton pattern
(b) While using the Factory pattern

In both cases only one constructor is being called -- the specific object constructor. The methods being invoked by clients in both cases are just ordinary responsibilities. In Singleton the method is usually implemented as a static class method. In Factory the method is just a routine responsibility of Factory class. As it happens, to implement the method responsibilities they need to invoke constructors.



************* 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: Abstract public member variales?
    ... The relationship is instantiated in the factory and remains ... Serializable.saveItwhen using the Serializer pattern. ... Context upon any one particular algorithm of a class of related ... responsibilities of Context; ...
    (comp.object)
  • Re: Dynamic inheritance
    ... >> to invoke the development portion of the IDE to modify the tree and ... > That's not the case in Smalltalk, ... properties at run time when the constructor is invoked. ... repository, modify the tree with the existing IDE tools, and then let ...
    (comp.object)
  • Re: Abstract Method w/o Factory
    ... >>It is fairly common for a Factory to instantiate both the instance of ... >>Client and the relationship. ... >>they isolate the rules and policies of instantiation in one place. ... that wants the instantiation responsibilities encapsulated AND isolated ...
    (comp.object)
  • Re: Trouble with factory pattern
    ... the Factory itself goes and gets ... The former is most useful when the Context has all the necessary data, ... but only some of it is needed for each constructor. ... The specification object is useful when the decision data is not readily ...
    (comp.object)
  • Re: Trouble with factory pattern
    ... the Factory itself goes and gets ... The former is most useful when the Context has all the necessary data, ... but only some of it is needed for each constructor. ... The specification object is useful when the decision data is not readily ...
    (alt.comp.lang.learn.c-cpp)