473,320 Members | 2,088 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,320 software developers and data experts.

accessor_cast

I've written a cast to mimic the way unions work. Here it is,
"accessor_cast.hpp":
#ifndef INCLUDE_ACCESSOR_CAST
#define INCLUDE_ACCESSOR_CAST

template<typename TO, class FROM>
inline TO& accessor_cast(FROM& from)
{
return *(reinterpret_cast<TO*>(&from));
}

#endif

---
Firstly, here's a union sample file:

union SomeUnion
{
unsigned long a;
char b;
double c;
short d;
};

int main()
{
SomeUnion poo;

poo.a = 4000000000UL;
poo.b = 't';
poo.c = 48656.6435;
poo.d = 27;
}

----
Now here it is without unions:
#include <iostream>

#include "accessor_cast.hpp"

int main()
{
double poo;

accessor_cast<unsigned long>(poo) = 4000000000UL;
std::cout << "unsigned long: "
<< accessor_cast<unsigned long>(poo)
<< std::endl;
accessor_cast<char>(poo) = 't';
std::cout << "char: " << accessor_cast<char>(poo) << std::endl;
accessor_cast<double>(poo) = 48656.6435;
std::cout << "double: " << accessor_cast<double>(poo) << std::endl;
accessor_cast<short>(poo) = 27;
std::cout << "short: " << accessor_cast<short>(poo) << std::endl;
//Or if you like:

unsigned long& counter = accessor_cast<unsigned long>(poo);

counter = 4000000000UL;

std::cout << "counter == " << counter << std::endl;

}
I haven't bothered testing it with const variables.
-JKop
Jul 22 '05 #1
2 1172
JKop <NU**@NULL.NULL> wrote in news:69*****************@news.indigo.ie:
I've written a cast to mimic the way unions work.

(...)

Unions are not meant as a tool for casting between types. "Casting through
a union" is just a method of fooling the compiler so it doesn't issue an
error message.
Jul 22 '05 #2
bartek posted:
JKop <NU**@NULL.NULL> wrote in news:69*****************@news.indigo.ie:
I've written a cast to mimic the way unions work.

(...)

Unions are not meant as a tool for casting between types. "Casting
through a union" is just a method of fooling the compiler so it doesn't
issue an error message.


I'm using it to access a certain piece of memory differently.
-JKop
Jul 22 '05 #3

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

Similar topics

71
by: cj | last post by:
Dear friends, I have one more questions for everyone in the newsgroup: I am preparing for an interview on UNIX/C++. Could you please identify some of the most important questions which might be...
5
by: johny smith | last post by:
I have never really understood the purpose of the void*. I guess it is so different data types could for instance be passed into a function. And then the function will re cast it to the correct...
13
by: xuatla | last post by:
I encountered "segmentation fault" and I checked my code, found the following problem: I want to reallocate memory for an array. I defined the following function: int reallocateMemory( double...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.