473,503 Members | 1,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

casting primitive type to user-defined type works in usage

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"

Apr 15 '06 #1
2 2146
xl***************@gmail.com 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".
Apr 15 '06 #2

red floyd wrote:
xl***************@gmail.com 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.

Apr 15 '06 #3

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

Similar topics

3
9774
by: ken | last post by:
I am getting this error from a gcc compile and I was wondering whether this was 100% valid. This seems a little extreme to me the c++ cast operators appear to only work on objects which defeats...
3
1825
by: Tony Johansson | last post by:
Hello! I'm reading in a document about window system and here they talk about primitive class. So my question is what is a primitive class? It might not mean any spacial just a name of a...
7
30750
by: Jim Bancroft | last post by:
Hi everyone, A basic one here, I think. I haven't found the pattern yet, but sometimes when I cast a variable to another type using the "C" style cast operator the compiler refuses to play...
61
4502
by: Ken Allen | last post by:
I am relatively new to .Net, but have been using VB and C/C++ for years. One of the drawbacks with VB6 and earlier was the difficulty in casting a 'record' to a different 'shape' so one could...
12
13622
by: 6tc1 | last post by:
Hi all, I just discovered a rounding error that occurs in C#. I'm sure this is an old issue, but it is new to me and resulted in a fair amount of time trying to track down the issue. Basically...
2
1712
by: cylt | last post by:
Hi, I would like to have something like that : IList<IUser> list2 = list1 as IList<IUser>; where list1 is a generic collection of User ( IList list1<User>=new List<User>() ) and where User...
2
2786
by: Rafael Giannetti Viotti | last post by:
Hi, I am working with the subprocess.py module in Python 2.4.4 and I am confused about it's functionality. It uses the standard pipe-fork-exec method to start a subprocess: # create pipes ...
15
1389
by: Tao Wang | last post by:
Hi, I have a class Data as following class Data{ .... }; I know if I want to cast it to int implicitly I should write like following:
101
4210
by: Tinkertim | last post by:
Hi, I have often wondered if casting the return value of malloc() (or friends) actually helps anything, recent threads here suggest that it does not .. so I hope to find out. For instance : ...
4
4383
by: AndreH | last post by:
Good day, I have the a bit of an issue with retrieving an object from php's session. I set a session variable "user" from the class User as follows: $user = new User(); // ... do some stuff...
0
7074
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...
1
6982
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5572
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5000
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4667
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3150
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1501
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
374
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.