Re: Me vs. MyClass



On 2005-09-06, Jay B. Harlow [MVP - Outlook] <Jay_Harlow_MVP@xxxxxxxxxxxxx> wrote:
> David,
>| Something else occurred to me: calling overridable functions from a
>| ctor. Seems pretty obscure though, and again it's providing language
>| support for a bad practice.
> ? puzzled look ?
>
> C# & I believe both Java & C++ all support calling overridable methods in
> constructors.

I should have been clearer. These languages all do this, but they do
it differently. As you point out, in .Net the vtable is built before
the derived classes have run their ctor, so you have the possibility of
an derived instance method called before the derived instance ctor.

In C++, the behavior is the opposite. The vtable for the derived class
is built during construction of the derived class, so a call to a
virtual method during a base class ctor calls the base class. The
contract of "no instance method is called before construction" holds
true.

MyClass could be used to emulate the C++ behavior. Is that why it's
there? Probably not, I'm really grasping at straws. My best guess at
this point is that its existence is related to the fact that vbc seems to
treat all instance calls as virtual. But I'm probably overthinking it,
it could just be that it was a cheap keyword to implement and somebody
thought it might be useful someplace.

BTW, Java acts like C#/VB in this case. I think it's kind of a GC thing.

> In fact System.Windows.Forms.Form.CreateParams is called from the
> constructor! Which is why CreateParams cannot rely on fields initialized in
> the constructor as the base constructor calls it before the derived
> constructor has a chance for it to run.
>
> I really don't see how MyClass adds or takes away support to calling
> overridable methods in constructors...
>
> Of course calling an overridable method in a constructor needs to be done
> with caution.
>
> Hope this helps
> Jay
>
>
> "david" <david@xxxxxxxxxxxxxxxx> wrote in message
> news:slrndhpm51.hs5.david@xxxxxxxxxxxxxxxxxxxxxxxx
>| On 2005-09-05, Jay B. Harlow [MVP - Outlook]
><Jay_Harlow_MVP@xxxxxxxxxxxxx> wrote:
>| > David,
>| > As I stated:
>| >
>| > It appears we may need to agree to disagree on MyClass & constructors...
>:-|
>|
>| Agreed.
>|
>| > If I find additional info on a viable reason to call MyClass outside of
>| > constructors I will post it.
>|
>| Something else occurred to me: calling overridable functions from a
>| ctor. Seems pretty obscure though, and again it's providing language
>| support for a bad practice.
>|
><<snip>>
>
>
.



Relevant Pages

  • Re: Me vs. MyClass
    ... and again it's providing language ... the constructor as the base constructor calls it before the derived ... I really don't see how MyClass adds or takes away support to calling ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Initialize an array of classes?
    ... Currently you cannot use member initialization list for a nonstatic array of a class if it does not have a default constructor. ... however you may encounter the warning C4351 and I do not think that it is useful to use such initialization for an array. ... Microsoft Online Community Support ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Windows Form Designer generated code
    ... In VS.NET 2003, the part of 'Windows Form Designer generated code' contains a constructor and the InitializeComponent method, and these code is located in the form.vb file. ... You could put your initialization code either in the constructor or the Load event handler of the form. ... The MSDN Managed Newsgroup support offering is for non-urgent issues where an initial response from the community or a Microsoft Support Engineer within 1 business day is acceptable. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Object oriented stuff and browsers related thing
    ... with Netscape 2.0 being the first browser to support ... iff MyConstr() is called as a constructor. ... It refers to `MyConstr' as that would be the ...
    (comp.lang.javascript)
  • Re: limited types (Was: Records that could be arrays)
    ... The constructor is wrapped by the function. ... you cannot ensure the wrapper will always be called for some ... Containers of class-wide types ... own ctor, if only to call the ctor of the base class. ...
    (comp.lang.ada)