473,396 Members | 2,115 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

C-style casts to subclass of polymorphic base

Does the following code exhibit undefined behavior?

#include <stdio.h>

class A
{
virtual int quux()=0;
};

class B : public A
{
int quux() {return(1);}
public:
unsigned int foo;
};

class C: public A
{
int quux() {return(0);}
public:
const char *bar;
};

void baz( const A &a )
{
((B*)&a)->foo=3; /* a is a C, not a B */
/* Prints 3, 00000000 */
printf( "%u, %p\n", ((B*)&a)->foo, (void *)dynamic_cast<const B*>(&a)) );
}

int main()
{
C b;
baz( b );
return 0;
}

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #1
3 1102
Christopher Benson-Manica <at***@nospam.cyberspace.org> spoke thus:
printf( "%u, %p\n", ((B*)&a)->foo, (void *)dynamic_cast<const B*>(&a)) );

(void *)(dynamic_cast<const B*>(&a))

Sorry about that.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #2

"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
news:ch**********@chessie.cirr.com...
Does the following code exhibit undefined behavior?

#include <stdio.h>

class A
{
virtual int quux()=0;
};

class B : public A
{
int quux() {return(1);}
public:
unsigned int foo;
};

class C: public A
{
int quux() {return(0);}
public:
const char *bar;
};

void baz( const A &a )
{
((B*)&a)->foo=3; /* a is a C, not a B */
/* Prints 3, 00000000 */
printf( "%u, %p\n", ((B*)&a)->foo, (void *)dynamic_cast<const B*>(&a)) ); }

int main()
{
C b;
baz( b );
return 0;
}


Sutter's GOTW has a similar example that claims that it is not portable.
See http://www.gotw.ca/gotw/076.htm, #3 (The Cheat). Do you really want to
use this code, or are you posing a hypothetical question about undefined
behavior? It should be obvious to most experienced C and C++ programmers
that the code is using a dirty hack. I don't know if anyone has the
patience to track down the exact quote in the standard that states why it is
a hack or invokes undefined behavior.

--
David Hilsee
Jul 22 '05 #3
David Hilsee <da*************@yahoo.com> spoke thus:
See http://www.gotw.ca/gotw/076.htm, #3 (The Cheat). Do you really want to
use this code, or are you posing a hypothetical question about undefined
behavior? It should be obvious to most experienced C and C++ programmers
that the code is using a dirty hack. I don't know if anyone has the
patience to track down the exact quote in the standard that states why it is
a hack or invokes undefined behavior.


I definitely do not want to use the code; I asked because some code
that I am working on that's mostly maintained by my boss blindly uses
a C-style cast to cast a pointer to a base to a particular subclass,
rather than using dynamic_cast and checking for failure. I wanted a
firm basis for telling him that the code is dangerous as written.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: tomek | last post by:
Can anybody explain why can I not get rid of the above message in the following class definition? Thank you in advance. class BasicMidReader : public TMemoryStream { //some private date...
2
by: Andrew G. J. Fung | last post by:
Can anyone please explain to me why the code below throws an exception? It seems to occur when deserializing an instance of a subclass, whose base class holds a struct, where said struct holds an...
12
by: st_ev_fe | last post by:
I've noticed that when constructing a subclass, the base class get's it's contructors called. Is there some way to avoid this? The base class has one variable, which gets initialised to 0. ...
3
by: Raider | last post by:
I have library with some class having no virtual functions and non virtual destructor. I want to add new member functions to this class by creating derived class and then use derived class...
8
by: Lou Pecora | last post by:
I've been scanning Python in a Nutshell, but this seems to be either undoable or so subtle that I don't know how to do it. I want to subclass a base class that is returned from a Standard Library...
2
by: Ryan Liu | last post by:
Hi, There is a method Test() implemented in base class and override in subclass. In base class itself, how to make sure this.Test() will just call implementation in base class only? ...
6
by: Me | last post by:
I need to be able to acces non-virtual members of sublcasses via a base class pointer...and without the need for an explicit type cast. I thought a pure virtual getPtr() that acts as a type cast...
4
by: Kurt Smith | last post by:
Hi List: Class inheritance noob here. For context, I have the following base class and subclass: class Base(object): def __init__(self, val): self.val = val
3
by: Rob | last post by:
I have these classes (elided methods): class Base { public: Base(string name) {...} }; class Derived : public Base {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.