C#: Why can't I pass 'this' as a constructor argument to a base class? -


you can't pass this argument base constructor - see e.g., c# language specification section 10.10.1 constructor initializers (last line on page).

i don't understand limitation , to. in c#, opposed c++, instance being constructed @ actual type , "everything works" (though of course not initialized; mean virtual functions called in derived class' constructor execute derived class' methods). base classes allowed call virtual methods on though derived class' override executed , derived class might not ready it; that's not ruled out. reason restriction?

(in c++ allowed 3 reasons. first, user or derived class supposed know doing. second, user of c++ supposed know doing. , third, if user doesn't know doing c++ philosophy give him rope requires hang himself, , facilitate him when ties knot. like philosophy!)

what i'm trying do, way, construct initialized list members in circularly-linked list. there's base class element field link point next element. there's class head derives element, it'll distinguished sentinel start of list plus have special behavior entire list. constructor element takes head of list argument, want write follows initialize elements , head properly:

class element {     protected element link;     public element(element prior)      {         this.link = null;         prior.link = this;     } };  class head : element {     public head() : base(this) {} }; 

(before complain should example differently actual code more complicated - specialized sparse array - , have reasons.) i'm going use factory lists (and factory method on head adding elements) avoid this, arguably better design, i'm puzzled other reasonable method outlawed.

the design of c# , .net intended give base class control on invariants. once base-class constructor has been completed, derived-class code, including derived-class constructors, allowed manipulate base-class portion of object in way allowed base class. derived class cannot object under construction other storing values own (derived-type) fields prior base-class constructor call, base class contract can in effect "i'm going call virtual method during constructor, , legitimate derived class must prepared deal that".

the design pose limitations. worst imho there no mechanism base class can assert control of construction process @ time after derived-class construction code gets access constructor parameters.

c# evaluates field initialization expressions before chaining base constructor, on theory allow derived-class objects set before base constructor calls virtual methods; consequence of field-initializer expressions can't object under construction. vb.net runs initializers after base constructor, allows more convenient access means fields unitialized if base constructor calls virtual methods. personally, regard c# approach "almost" useful, inability handle classes invariants affected constructor parameters severely limits usefulness.

what i'd see, btw, object include virtual method run between time derived constructor finishes execution or throws exception , time control returns code calls foo = new bar();. primary reason base-class constructors expose objects under construction before derived-class constructors run there no standard way them ensure they'll ever control after that. such design improve constructor sequencing. i'm not holding breath such thing, though.


Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -