Topic: When is vtable supposed to be set?


Author: "Robert J. Feldbruegge" <rjfeldbruegge@mke4.ra.rockwell.com>
Date: 1996/05/09
Raw View
I have some code that I'm porting to the PC, and have run into a
discrepency between the assembly code produced by our embedded compiler
and that produced by the Microsoft compiler.

I am curious as to which is the "right" implementation as defined in the
C++ standards.

My question is, when should the vtable be set for a derived class during
construction?

The embedded compiler's code for the derived constructor does the
following:
1. call the base class constructor
2. set the vtable for the derived class
3. call initializors

The Microsoft compiler's code for the derived constructor does the
following:
1. call the base class constructor
2. call initializors
3. set the vtable for the derived class

My code, simplified, is basically as follows:

class B {
   virtual void mFunc(void) = 0;
};

class C {
   C(B* pB);
};

class D : public B {
   C   m_var;
   void mFunc(void);
};

D::D() : m_var(this) {}

C::C(B* pB) { pB->mFunc(); }

This code works from the embedded compiler, but not from Microsoft's.
The reason is because under Microsoft, D's constructor tries to
initialize m_var before setting the vtable, thus pB->mFunc is a call to a
pure virtual function.

Any help, hints, etc. would be greatly appreciated.

TIA!

Rob

============================================================
   Robert J. Feldbruegge <rjfeldbruegge@mke4.ra.rockwell.com>
   Senior Software Engineer

Rockwell Automation
------------------------------------------------------------
   Allen-Bradley Company

   Milwaukee Wisconsin
============================================================


[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1996/05/09
Raw View
In article 225@mke4.ra.rockwell.com, "Robert J. Feldbruegge" <rjfeldbruegge@mke4.ra.rockwell.com> writes:
>I am curious as to which is the "right" implementation as defined in the
>C++ standards.
>
>My question is, when should the vtable be set for a derived class during
>construction?
>
>The embedded compiler's code for the derived constructor does the
>following:
>1. call the base class constructor
>2. set the vtable for the derived class
>3. call initializors
>
>The Microsoft compiler's code for the derived constructor does the
>following:
>1. call the base class constructor
>2. call initializors
>3. set the vtable for the derived class

Of course the draft standard does not mention vtables, but it is
nevertheless clear about what should happen. See section 12.7,
"Construction and destruction" [class.cdtor].

During the course of construction or destruction, including a
mem-initializer, the typeid and dynamic_cast operators return
a result based on the constructor's or destructor's own class.
The type of the object, even during execution of a mem-initializer,
is therefore the type of the constructor's or destructor's class,
and the version of the virtual function associated with that
class should be called. Thus, the embedded compiler appears to do
the right thing, and the Microsoft compiler appears to be incorrect.

Note, however, that the object does not take on the constructor's type
until all of its base classes have been initialized. Attempting
to treat the object as the constructor's type before all base classes
have been initialized has undefined results. Your example seems to
conform to these constraints.
---
Steve Clamage, stephen.clamage@eng.sun.com
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]