Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / November 2007

Tip: Looking for answers? Try searching our database.

Type coercion

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bob Altman - 28 Nov 2007 20:21 GMT
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
Bob Altman - 28 Nov 2007 20:31 GMT
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

> Hi all,
>
[quoted text clipped - 14 lines]
>
> TIA - Bob
Ben Voigt [C++ MVP] - 28 Nov 2007 20:40 GMT
> 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
[quoted text clipped - 19 lines]
>>
>> TIA - Bob
Ben Voigt [C++ MVP] - 28 Nov 2007 20:32 GMT
> Hi all,
>
[quoted text clipped - 12 lines]
> 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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.