From: "Martin v. Loewis" <martin@mira.isdn.cs.tu-berlin.de>
To: ulisses@watson.ibm.com
Cc: gcc@gcc.gnu.org
Subject: Re: static_cast + virtual table = problems ?
Date: Wed, 08 Sep 1999 00:09:00 -0000 [thread overview]
Message-ID: <199909080704.JAA00676@mira.isdn.cs.tu-berlin.de> (raw)
In-Reply-To: <Pine.A32.3.93.990907200518.27690E-100000@jacare.watson.ibm.com>
> 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
WARNING: multiple messages have this Message-ID
From: "Martin v. Loewis" <martin@mira.isdn.cs.tu-berlin.de>
To: ulisses@watson.ibm.com
Cc: gcc@gcc.gnu.org
Subject: Re: static_cast + virtual table = problems ?
Date: Thu, 30 Sep 1999 18:02:00 -0000 [thread overview]
Message-ID: <199909080704.JAA00676@mira.isdn.cs.tu-berlin.de> (raw)
Message-ID: <19990930180200.0XUdwBMj5bUDfXtKq54DBfsIQA8CjLaeIYIwm3lhB3M@z> (raw)
In-Reply-To: <Pine.A32.3.93.990907200518.27690E-100000@jacare.watson.ibm.com>
> 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
next prev parent reply other threads:[~1999-09-08 0:09 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
1999-09-07 17:13 Ulisses T. Mello
1999-09-08 0:09 ` Martin v. Loewis [this message]
1999-09-30 18:02 ` Martin v. Loewis
1999-09-30 18:02 ` Ulisses T. Mello
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=199909080704.JAA00676@mira.isdn.cs.tu-berlin.de \
--to=martin@mira.isdn.cs.tu-berlin.de \
--cc=gcc@gcc.gnu.org \
--cc=ulisses@watson.ibm.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).