473,569 Members | 2,834 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<<"Bir d::IFly"<<std:: endl;
std::cout<<"age : "<<age<<std::en dl;
}

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

return 0;
}

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

Apr 15 '06 #1
2 2147
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
9779
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 the purpose of them. I had to remove the C++ cast and change to a C one in order for gcc to accept it, we are using permissive and I am getting rid of...
3
1831
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 class. //Tony
7
30765
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 along. It says it's an invalid cast. However, if I use the Convert.ToInt32() method (for example) things will work. At least, that's how it appears to...
61
4524
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 perform different manipulations on it. For example, I have a complex data structure, which I can represent in a VB6 TYPE declaration, but I cannot...
12
13631
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 put the following code into your C# app: float testFloat2 = (int) (4.2f * (float)100); Console.Out.WriteLine("1: "+testFloat2); and the result...
2
1718
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 implement the interface IUser. But this did not work... I thought that as User implement IUser it was possible to cast the generic
2
2790
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 pid = fork()
15
1394
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
4244
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 : char *tmp = NULL;
4
4389
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 to $user $_SESSION = serialize($user);
0
7693
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7605
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7917
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7665
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6277
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2105
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 we have to send another system
0
933
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.