C++ question

Allan Sandfeld Jensen snowwolf at one2one-networks.com
Wed Sep 25 13:34:00 BST 2002


On Wednesday 25 September 2002 13:35, David Faure wrote:
> On Wed, 25 Sep 2002, Michael Goffioul wrote:
> > Can I call a pure virtual function in the constructor of the abstract
> > class? That is
> >
> > class A
> > {
> > public:
> >    A() { func(); }
> >    virtual void func() = 0;
> > };
>
> Impossible. When A is built, there is not yet a reimplementation of func()
> registered in the vtable. This is a good way to see the nice "pure virtual
> method called".
> You have to simplify the constructors, and add some virtual init() that
> does the job, for instance.
>
Well almost impossible. rtti is active. So in case you know all possible 
children, you can just write a switch to call the right function.

Infact since rtti is active, some compilers might support calling virtual 
functions in constructors. It would just be a special case and non-standard 
ofcouse.
( I guess since so many people writes it is impossible, that gcc havent got 
that extension )




More information about the kde-core-devel mailing list