Re: Why can't I assign "this" in a constructor?

From: Tor Iver Wilhelmsen (tor.iver.wilhelmsen_at_broadpark.no)
Date: 12/27/03


Date: 27 Dec 2003 12:13:26 +0100

greg_finch2@hotmail.com (Thinkit) writes:

> It seems like a basic thing you'd want to do. It seems it only lets
> you call an alternate constructor, this(...), as the first line in the
> method. I want to be able to just say this=new MyObject(...) or
> this=this.add(...).

Because constructors are used from the outside: If the caller wants a
particular MyObject they can call the proper constructor.

An calling this() is't calling an _alternate_ constructor, but is
known as constructor chaining. The constructor you're in is still
_the_ constructor invoked.

> I could just use a static method, but why don't I have the choice?

Because it's insane? You're trying to extend a constructor's role to
being a factory method, which is not what it's for: It's role is to
initiate *one* particular instance, referenced via the value this.



Relevant Pages

  • Re: Why cant I assign "this" in a constructor?
    ... > you call an alternate constructor, this, as the first line in the ... memory allocation overloading before explicit overloading of the 'new' ...
    (comp.lang.java.programmer)
  • Re: Unusual usage of IUknown
    ... ptA = new A; ... calling V constructor, szData=V ... calling A destructor, szData=A ...
    (microsoft.public.vc.language)
  • Re: Unusual usage of IUknown
    ... char * szData; ... calling V constructor, szData=V ... calling A destructor, szData=A ...
    (microsoft.public.vc.language)
  • Re: Object passing
    ... crash MATLAB and/or your computer. ... Your constructor is ... calling something else which is calling the constructor ... classdef b < handle ...
    (comp.soft-sys.matlab)
  • Re: Assembly problem
    ... Theodore ... > System.Windows.Forms is that the method you are calling GetCallingAssembly ... > the constructor is being invoked by assembly A. If A does not construct ... >> which in turn calls class B located in another dll file. ...
    (microsoft.public.dotnet.general)