Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > casting primitive type to user-defined type works in usage

Reply
Thread Tools

casting primitive type to user-defined type works in usage

 
 
xllx.relient.xllx@gmail.com
Guest
Posts: n/a
 
      04-15-2006
Can anyone kindly explain why the following works, even though it's
undoubtedly a very bad thing to do:

class Bird
{
int age;

public:
Bird(int = 21);
void IFly(void);
};

Bird::Bird(int lage)
{
age = lage;
}

void Bird::IFly()
{
age = 21;
std::cout<<"Bird::IFly"<<std::endl;
std::cout<<"age: "<<age<<std::endl;
}

int main(int argc, char **argv)
{
int i;
Bird *x = reinterpret_cast<Bird*>(&i);
x->IFly();

return 0;
}

PRINTS: "Bird::IFly"
"age: 21"

 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      04-15-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Can anyone kindly explain why the following works, even though it's
> undoubtedly a very bad thing to do:
>
>[code redacted]


Because undefined behavior can do anything, including seeming to "work".


 
Reply With Quote
 
 
 
 
xllx.relient.xllx@gmail.com
Guest
Posts: n/a
 
      04-15-2006

red floyd wrote:
> (E-Mail Removed) wrote:
> > Can anyone kindly explain why the following works, even though it's
> > undoubtedly a very bad thing to do:
> >
> >[code redacted]

>
> Because undefined behavior can do anything, including seeming to "work".


Good enough, Thanks.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange behavior with casting primitive types Gaijinco C++ 5 03-09-2009 09:41 AM
Default primitive values from primitive Class<?> object. Daniel Pitts Java 7 10-23-2008 04:30 PM
Primitive vs. non-primitive l-value richardclay09@yahoo.co.uk C++ 7 05-09-2005 02:52 PM
Return by value -- primitive type vs class type DaKoadMunky C++ 8 05-14-2004 09:46 PM
Re: Type casting- a larger type to a smaller type heyo C Programming 3 04-01-2004 06:35 PM



Advertisments