This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

g++: virtual function call failes in constructor


Dear Madam, dear Sir,

here all the information you need to for your bug report:

Originator: Martin Weber

Organization: RWTH Aachen

Confidential: no

Synopsis: virtual function call failes in constructor

Severity: serious

Priority: medium

Category: c++

Class: wrong-code

Release: gcc -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/egcs-2.91.66/specs
gcc version egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)

Environment: i386-redhat-linux Linux version 2.2.12-32

Description:

The call to the virtual function Print() in A::A() is not resolved to
B::Print() in the statement B b; as it is expected for a virtual function.
The output of the program is expected to be

A::Print()
B::Print()

but instead is

A::Print()
A::Print()

How-To-Repeat: (source code)
// virtualtest.cxx
#include <iostream>

class A 
{
public:
  A() { A * sa = this; sa->Print(); }
  virtual ~A() {}
  virtual void Print() { cout << "A::Print()" << endl; }
  void Test() { Print(); }
};

class B : public A
{
public:
  B() {  }
  ~B() {}
  void Print() { cout << "B::Print()" << endl; }
};

main()
{
  A a;
  B b;
}

command line: g++ -Wall virtualtest.cxx -o virtualtest
              ./virtualtest

compiler output: none

preprocessed file: as attachment

Fix: call your virtual functions somewhere else

Best regards,

Martin Weber

---
 Martin Weber
 III. Physikalisches Institut    Fon: +49-241-80-6354
 RWTH Aachen                     Fax: +49-241-8888-189
 52056 Aachen                 e-mail: Martin.Weber@physik.rwth-aachen.de

virtualtest.ii.gz


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]