[Date Prev][Date Next][Thread Prev][Thread Next][Author Index][Date Index][Thread Index]

Re: Virtual functions used in constructor illegal ! ??

> From: roger (Roger Gregory)
> yow!! is this true markm?
> In article <27556AD1.350C@xxxxxxxx>  chip@xxxxxxxx write:
> >According to jbuck@xxxxxxxxxxxxxxxxxxxx (Joe Buck):
> >>[E&S] actually says that if you do call a virtual method in the
> >>constructor, that the base class method is always called, even
> >>if you redefine the method.
> >
> >This is true, but only if you are calling the virtual function from
> >the actual constructor function body.  If you call a virtual function
> >from a constructor by other means, such as calling a function that
> >calls the virtual function, E&S says that the behavior is undefined,
> >i.e. all bets are off.

No, it's not.  E&S PP 233:

  "... The first point at which C::f() can be called as f() is the
  first statement of the constructor C::C()."

The poster is confused about where he's standing when he says "base class",
and thus misreading what E&S are saying when they're talking about a
constructor >in a base class< not having access to the functions >of
the classes derived from the base class<, which aren't yet constructed.

I haven't found anything in E&S that says behavior is undefined if
you call a function that calls a virtual function in a partially-
constructed object.  The code generated by cfront does the right
thing (updates the vpointer just before executing the first statement
of the constructor, and just after executing the last statement of the
destructor).  I suspect this is a true reflection of the semantics,
not an accident.