473,396 Members | 1,864 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.

Type coercion

Hi all,

I have two C++ structures. Both structures include a parameterless
constructor that initializes the structure members. These two structures
define different ways of looking at the same data; one structure exposes the
data as an array of bytes, the other exposes the bytes as individual
structure members. However, because of the constructors, I can't create a
higher-level struct that unions the two structures together. (The
structures in a union can not have a user-supplied default constructor.)

So, here's my question: If I have an instance of one structure type, is
there a way to tell C++ to treat it as though it is an instance of the other
structure type without having to create an overloaded "=" operator that
moves bytes in memory? The reinterpret_cast operator would seem to be just
what I want, but it refuses to cast one type to a totally unrelated type.

TIA - Bob
Nov 28 '07 #1
3 1872
Never mind... I left a magic "&" off of the reinterpret_cast syntax that I
used in my code. I found an excellent writeup on the general subject of
"type punning" (yes, that's "pun" as in "play on words") here:

ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VSADD.v10.en/
dndeepc/html/deep06012000.htm

- Bob

"Bob Altman" <rd*@nospam.nospamwrote in message
news:uz**************@TK2MSFTNGP03.phx.gbl...
Hi all,

I have two C++ structures. Both structures include a parameterless
constructor that initializes the structure members. These two structures
define different ways of looking at the same data; one structure exposes
the data as an array of bytes, the other exposes the bytes as individual
structure members. However, because of the constructors, I can't create a
higher-level struct that unions the two structures together. (The
structures in a union can not have a user-supplied default constructor.)

So, here's my question: If I have an instance of one structure type, is
there a way to tell C++ to treat it as though it is an instance of the
other structure type without having to create an overloaded "=" operator
that moves bytes in memory? The reinterpret_cast operator would seem to
be just what I want, but it refuses to cast one type to a totally
unrelated type.

TIA - Bob

Nov 28 '07 #2

"Bob Altman" <rd*@nospam.nospamwrote in message
news:uz**************@TK2MSFTNGP03.phx.gbl...
Hi all,

I have two C++ structures. Both structures include a parameterless
constructor that initializes the structure members. These two structures
define different ways of looking at the same data; one structure exposes
the data as an array of bytes, the other exposes the bytes as individual
structure members. However, because of the constructors, I can't create a
higher-level struct that unions the two structures together. (The
structures in a union can not have a user-supplied default constructor.)

So, here's my question: If I have an instance of one structure type, is
there a way to tell C++ to treat it as though it is an instance of the
other structure type without having to create an overloaded "=" operator
that moves bytes in memory? The reinterpret_cast operator would seem to
be just what I want, but it refuses to cast one type to a totally
unrelated type.
(1) reinterpret_cast the address as a pointer to the other type

(2) use a union, and put the initialization in the constructor for the union
itself
Nov 28 '07 #3

"Bob Altman" <rd*@nospam.nospamwrote in message
news:us**************@TK2MSFTNGP06.phx.gbl...
Never mind... I left a magic "&" off of the reinterpret_cast syntax that
I used in my code. I found an excellent writeup on the general subject of
"type punning" (yes, that's "pun" as in "play on words") here:
I still think you should use the union (see my other post), as the layout of
a non-POD type isn't necessarily sequential (that's why they can't be used
inside a union).
>
ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VSADD.v10.en/
dndeepc/html/deep06012000.htm

- Bob

"Bob Altman" <rd*@nospam.nospamwrote in message
news:uz**************@TK2MSFTNGP03.phx.gbl...
>Hi all,

I have two C++ structures. Both structures include a parameterless
constructor that initializes the structure members. These two structures
define different ways of looking at the same data; one structure exposes
the data as an array of bytes, the other exposes the bytes as individual
structure members. However, because of the constructors, I can't create
a higher-level struct that unions the two structures together. (The
structures in a union can not have a user-supplied default constructor.)

So, here's my question: If I have an instance of one structure type, is
there a way to tell C++ to treat it as though it is an instance of the
other structure type without having to create an overloaded "=" operator
that moves bytes in memory? The reinterpret_cast operator would seem to
be just what I want, but it refuses to cast one type to a totally
unrelated type.

TIA - Bob


Nov 28 '07 #4

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

Similar topics

1
by: Harald Kirsch | last post by:
How would I call a java constructor which takes a java.lang.CharSequence as argument. Say, the java class goes like this class X { public X(CharSequence s) { ... } } In the jython...
21
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing/modifying the code. Thank you. .. Facundo
10
by: Mark Sargent | last post by:
Hi All, why am I getting this error..? Cheers. Mark Sargent. Error Type: Microsoft VBScript runtime (0x800A000D) Type mismatch: ''
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
9
by: ThunderMusic | last post by:
Hi, I'm doing comparisons over arguments passed as Object... they can be of any type, but must be comparable using < or >... unfortunatly, the compiler does not give the possibility to compare 2...
59
by: peter.tornqvist | last post by:
Maybe I'm stupid or maybe I am missing the obvious, but I can't find a way to define a type alias in C# for primitive types (class and interfaces I can inherit from, no problem). I.e I want to...
0
by: mazerj2006 | last post by:
I'm an old hand at python, but totally new to win32com -- I've run into a snag that seems to be related to python's (usually handy) lack of distinction between floats and doubles. Can anyone...
9
by: AGP | last post by:
I've been scratching my head for weeks to understand why some code doesnt work for me. here is what i have: dim sVal as string = "13.2401516" dim x as double x = sVal debug.writeline ( x)
2
by: Paul Moore | last post by:
I'm trying to implement an extension type with a power operator. The operator is unusual in that I want to allow my objects to be raised to an integer power: p = Pattern() p3 = p ** 3 I've...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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,...
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
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
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...
0
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 projectplanning, coding, testing,...

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.