Topic: vtable installation


Author: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
Date: 1996/06/10
Raw View
dak <pierreba@poster.cae.ca> writes:

>I've got a question on when the vtable for a particular class is installed.
>I would believe that for a class D the vtable is installed after the base
>class are initialized but before any initializer list member-data is called.

The draft C++ standard doesn't talk about vtables, but yes, that's
basically correct.

>On xlC and g++ I have the following output (what I thought):
>
>V
>D
>E::f
>E
>d = 1
>e = 0
>
>while on Visual C++ 4.0, I have the following (surprise):
>
>V
>D
>D::f
>E
>d = 1
>e = 0

>From a brief look at it, I can't see any reason why your program
would have undefined behaviour; I think it is a bug in Visual C++ 4.0.
However, your example is sufficiently complicated (as are the
relevent sections of the draft standard) that it would require
quite a bit of study to be sure.

--
Fergus Henderson <fjh@cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3         |     -- the last words of T. S. Garp.
---
[ 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
]





Author: dak <pierreba@poster.cae.ca>
Date: 1996/06/07
Raw View

I've got a question on when the vtable for a particular class is installed.
I would believe that for a class D the vtable is installed after the base
class are initialized but before any initializer list member-data is called.
I have compilers that do not give the same answer and thus am not sure
anymore. Here a sample code showing the problem:

--- cut here ---

#include <iostream.h>

class V
{
public:
 V () { cout << "V" << endl; }
 virtual ~V () { cout << "~V" << endl; }
 virtual void f () = 0;
};

class D;
int g (D * apD);

class D : public virtual V
{
public:
 int d;
 D () : d ( g (this) ) { cout << "D" << endl; }
 virtual ~D () { cout << "~D" << endl; }
 virtual void f () { cout << "D::f" << endl; }
};

int g(D * apD)
{
 static V * pV = 0;
 if ( apD )
 {
  pV = apD;
  return 1;
 }
 else
 {
  pV -> f ();
  return 0;
 }
}

class E : public D, public virtual V
{
public:
 int e;
 E () : e ( g (0) ) { cout << "E" << endl; }
 virtual ~E () { cout << "~E" << endl; }
 virtual void f () { cout << "E::f" << endl; }
};

int main ()
{
 E * pE = new E;
 cout << "d = " << pE -> d << endl;
 cout << "e = " << pE -> e << endl;
 return 0;
}


-- cut here --

Note that the g() function is called by the initializer of the d data-member
of the D class ( which initialize the static variable and doesn't call f).
Note also that g() is called in the initializer of the e data-member of the
E class and then call the f() virtual function of the saved pointer.

I would have thought that the E vtable would be installed at that last point
and thus the implementation of the E class used.  But one compiler does not
agree. My question is that the requirement that the vtable be installed after
the call to the super-class constructors but before the constructor of the
class means before any data-member initializer is called ?

On xlC and g++ I have the following output (what I thought):

V
D
E::f
E
d = 1
e = 0

while on Visual C++ 4.0, I have the following (surprise):

V
D
D::f
E
d = 1
e = 0



[ 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                             ]