* Victor Bazarov:[color=blue]
>
PengYu.UT@gmail.com wrote:[color=green]
> > A pure function is called in the base function constructor. It
> > generate a run time error: "pure virtual method called".
> >
> > My problem is that class A have some derived classes. I want A's
> > constructor change its behaviour accounting to the derived class.
> >
> > I tried to make A::fun() not pure virtual but virtual. It doesn't
> > generate any error. But A::fun() is called in A's construction, while
> > I want B::fun() be called. I just don't want to define a default
> > virtual function.[/color]
>
> What you're trying to accomplish goes against a very basic principle:
> member functions shall not be called for an object whose construction
> hasn't completed. If you're trying to call B::fun from A::A, the B
> object hasn't finished constructing. Java is notoriously bad about
> this particular behaviour and it is a constant cause of trouble.
>[color=green]
> > I'm wondering if there is any work around to solve this problem.[/color]
>
> A work-around is a separate (maybe virtual) "initialise" member
> function, which will be called by the _user_ of your A or B class,
> after creating the object.[/color]
Don't do that: it allows for zombie objects, double init and whatnot.
See FAQ item 23.4.
[color=blue]
> If you want that sequence (construct,
> then initialise) to be executed without client's knowing about it,
> you need a factory (or several).[/color]
Nope.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?