public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* static_cast + virtual table = problems ?
@ 1999-09-07 17:13 Ulisses T. Mello
  1999-09-08  0:09 ` Martin v. Loewis
  1999-09-30 18:02 ` Ulisses T. Mello
  0 siblings, 2 replies; 4+ messages in thread
From: Ulisses T. Mello @ 1999-09-07 17:13 UTC (permalink / raw)
  To: gcc

static_cast is producing inconsistent results in gcc 2.95.1:

The following code:

#include <iostream>

class A {
public:
  A(int a): _a(a) {}
  friend ostream& operator << (ostream& os, const A& o) { 
    os << "a=" << o._a;
    return os; 
  }
  virtual void print() const { cout << *this << endl; } 

private:
  int _a;
};

class B: public A {
public:
  B(int a, int b) : A(a), _b(b) {}
  friend ostream& operator << (ostream& os, const B& o) { 
    os << static_cast<const A&>(o) << endl;
    os << "b=" << o._b << endl;
    return os; 
  }
  virtual void print() const { cout << *this << endl; } 
private:
  int _b;
};

int main() {
  B ba(1,2);
  static_cast<const A&>(ba).print();
  cout << static_cast<const A&>(ba) << endl;
  return 0;
}


a=1
b=2  <-- why b is being printed ???

a=1

If print() is declarad as non-virtual it produces the correct result.

ulisses.



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~1999-09-30 18:02 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-09-07 17:13 static_cast + virtual table = problems ? Ulisses T. Mello
1999-09-08  0:09 ` Martin v. Loewis
1999-09-30 18:02   ` Martin v. Loewis
1999-09-30 18:02 ` Ulisses T. Mello

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).