This is the mail archive of the gcc-help@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]
Other format: [Raw text]

Wrong virtual method called (bug?)


The following program gives different output on g++ 3.3.6 and 4.0.3. With 3.3.6 it prints

D::foo()
D::foo()

with 4.0.3 it prints

C::foo()
D::foo()

which is what I'd expected it to print. Is this a known issue?

Michael


#include <iostream> using namespace std;

struct B {
  virtual void foo() const = 0;
};

struct C: B {
  virtual void foo() const {  cout << "C::foo()" << endl;  }
};

struct D: B {
  virtual void foo() const {  cout << "D::foo()" << endl;  }
};

struct A {
  const B &b;
  A(const B& x = C()): b(x) {}
  void foo() {  b.foo();  }
};

int main() {
  A a1 = A(C());
  A a2 = A(D());
  a1.foo();
  a2.foo();
  return 0;
}



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