* 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
* Re: static_cast + virtual table = problems ?
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
1 sibling, 1 reply; 4+ messages in thread
From: Martin v. Loewis @ 1999-09-08 0:09 UTC (permalink / raw)
To: ulisses; +Cc: gcc
> a=1
> b=2 <-- why b is being printed ???
>
> a=1
>
> If print() is declarad as non-virtual it produces the correct result.
You get the correct result in both cases :-) A static cast is just
that: it changes the static type of some expression.
It does not make virtual functions non-virtual, so if you invoke a
virtual function on an A&, and the dynamic object is a B instance, you
get the B method.
To always invoke the A method, you have to write
ba.A::print();
instead.
Hope this helps,
Martin
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: static_cast + virtual table = problems ?
1999-09-08 0:09 ` Martin v. Loewis
@ 1999-09-30 18:02 ` Martin v. Loewis
0 siblings, 0 replies; 4+ messages in thread
From: Martin v. Loewis @ 1999-09-30 18:02 UTC (permalink / raw)
To: ulisses; +Cc: gcc
> a=1
> b=2 <-- why b is being printed ???
>
> a=1
>
> If print() is declarad as non-virtual it produces the correct result.
You get the correct result in both cases :-) A static cast is just
that: it changes the static type of some expression.
It does not make virtual functions non-virtual, so if you invoke a
virtual function on an A&, and the dynamic object is a B instance, you
get the B method.
To always invoke the A method, you have to write
ba.A::print();
instead.
Hope this helps,
Martin
^ permalink raw reply [flat|nested] 4+ messages in thread
* static_cast + virtual table = problems ?
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 ` Ulisses T. Mello
1 sibling, 0 replies; 4+ messages in thread
From: Ulisses T. Mello @ 1999-09-30 18:02 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).