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

a C++ question about union

JDT
NM_UPDOWN

With UDN_DELTAPOS notification handler, MS passes a pointer to NMHDR
which yo can cast to NM_UPDOWN*. With NMHDR (shown below) , you can use
"iDelta" to know the amount it spins while with NMHDR you can use "code"
to know the event type. What I don't understand is why the same storage
(the overlap of the last int and unsigned int) can provide two different
values by casting. Is it not union, is it? This is a C++ (or even a C)
question. If I underhand it, I may exploit the same tech for my own.
Your advise is appreciated. JD

typedef struct _NM_UPDOWN
{
NMHDR hdr;
int iPos;
int iDelta;
} NMUPDOWN, *LPNMUPDOWN;

typedef struct tagNMHDR
{
HWND hwndFrom;
UINT_PTR idFrom;
UINT code; // NM_ code
} NMHDR;
Apr 23 '07 #1
7 3890
JDT wrote:
NM_UPDOWN

With UDN_DELTAPOS notification handler, MS passes a pointer to NMHDR
which yo can cast to NM_UPDOWN*. With NMHDR (shown below) , you can
use "iDelta" to know the amount it spins while with NMHDR you can use
"code" to know the event type. What I don't understand is why the
same storage (the overlap of the last int and unsigned int) can
provide two different values by casting. Is it not union, is it? This is
a C++ (or even a C) question. If I underhand it, I may
exploit the same tech for my own. Your advise is appreciated. JD

typedef struct _NM_UPDOWN
{
NMHDR hdr;
int iPos;
int iDelta;
} NMUPDOWN, *LPNMUPDOWN;

typedef struct tagNMHDR
{
HWND hwndFrom;
UINT_PTR idFrom;
UINT code; // NM_ code
} NMHDR;
So much MS stuff here, my head hurts... Perhaps next time you will
rephrase to avoid using their abbreivations and all-capital-letters
names... It would be so nice...

Anyway, what you're asking about is akin to the fact that if I have
a "hierarchy" like this

struct A {
int a;
};

struct B {
A a;
double d;
};

, I could do the following:

#include <iostream>
void foo(A* pa) {
B* pb = (B*) pa;
std::cout << "The 'd' is " << pb->d << std::endl;
}

int main() {
B b = { { 42 }, 3.14159 };
foo(& b.a);
}

Well, yes, that's "allowed". The layout of any POD object is pretty
much preset, in the sense that 'd' follows 'a' in a 'B' and that the
address of 'a' in a 'B' is the same as the address of the 'B' itself.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 23 '07 #2
JDT wrote:
NM_UPDOWN

With UDN_DELTAPOS notification handler, MS passes a pointer to NMHDR
which yo can cast to NM_UPDOWN*. With NMHDR (shown below) , you can use
"iDelta" to know the amount it spins while with NMHDR you can use "code"
to know the event type. What I don't understand is why the same storage
(the overlap of the last int and unsigned int) can provide two different
values by casting. Is it not union, is it? This is a C++ (or even a C)
question. If I underhand it, I may exploit the same tech for my own.
Your advise is appreciated. JD
It's not a technique, it's a horrible kludge.

--
Ian Collins.
Apr 23 '07 #3
JDT <jd*******@yahoo.comwrote in news:_x7Xh.7473$H_5.4975
@newssvr23.news.prodigy.net:
NM_UPDOWN

With UDN_DELTAPOS notification handler, MS passes a pointer to NMHDR
which yo can cast to NM_UPDOWN*. With NMHDR (shown below) , you can
use
"iDelta" to know the amount it spins while with NMHDR you can use
"code"
to know the event type. What I don't understand is why the same
storage
(the overlap of the last int and unsigned int) can provide two
different
values by casting. Is it not union, is it? This is a C++ (or even a
C)
question. If I underhand it, I may exploit the same tech for my own.
Your advise is appreciated. JD

typedef struct _NM_UPDOWN
{
NMHDR hdr;
int iPos;
int iDelta;
} NMUPDOWN, *LPNMUPDOWN;

typedef struct tagNMHDR
{
HWND hwndFrom;
UINT_PTR idFrom;
UINT code; // NM_ code
} NMHDR;
This is more of a C question than C++, but here goes:
Note that the first member of _NM_UPDOWN is an NMHDR.

So what MS is doing behind the scenes is something like:
{
_NM_UPDOWN notification;

notification.hdr.code = NM_WhateverCode;

CallNotificationFunction(reinterpret_cast<NMHDR *>(&notification);
}
Basically it's relying on the fact that the first part of the NM_UPDOWN
struct actually is a NMHDR object. It's kinda trying to implement
inheritance in the C world.

So when MS is passing you a pointer to NMHDR, it's really passing you a
pointer to the hdr member of an NM_UPDOWN struct. Keep in mind that the
pointer to the first member of a struct is the same as a pointer to the
struct itself.
Apr 23 '07 #4
Ian Collins wrote:
JDT wrote:
>NM_UPDOWN

With UDN_DELTAPOS notification handler, MS passes a pointer to NMHDR
which yo can cast to NM_UPDOWN*. With NMHDR (shown below) , you can
use "iDelta" to know the amount it spins while with NMHDR you can
use "code" to know the event type. What I don't understand is why
the same storage (the overlap of the last int and unsigned int) can
provide two different values by casting. Is it not union, is it?
This is a C++ (or even a C) question. If I underhand it, I may
exploit the same tech for my own. Your advise is appreciated. JD
It's not a technique, it's a horrible kludge.
[Mostly directed at the OP]

IOW, there are more reliable ways to do that in C++. However, in C,
since there is no inheritance, relying on certain relationship
between a pointer to a struct member and and the struct object itself
is not completely unheard of. Think "offsetof" macro.

And since we are in a C++ newsgroup, there is probably more merit in
stating what problem you're trying to solve, so that a solution or
two (or three) can be proposed, instead of showing some other solution
to some other problem in an attempt to see if it solves something else
you have not encountered yet...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 23 '07 #5
Victor Bazarov wrote:
Ian Collins wrote:
>>JDT wrote:
>>>NM_UPDOWN

With UDN_DELTAPOS notification handler, MS passes a pointer to NMHDR
which yo can cast to NM_UPDOWN*. With NMHDR (shown below) , you can
use "iDelta" to know the amount it spins while with NMHDR you can
use "code" to know the event type. What I don't understand is why
the same storage (the overlap of the last int and unsigned int) can
provide two different values by casting. Is it not union, is it?
This is a C++ (or even a C) question. If I underhand it, I may
exploit the same tech for my own. Your advise is appreciated. JD

It's not a technique, it's a horrible kludge.


[Mostly directed at the OP]

IOW, there are more reliable ways to do that in C++. However, in C,
since there is no inheritance, relying on certain relationship
between a pointer to a struct member and and the struct object itself
is not completely unheard of. Think "offsetof" macro.
It's still a horrible kludge. If you want to do such things in C, use a
union of structures with the first member of each struct being the type
identifier.

--
Ian Collins.
Apr 23 '07 #6
Ian Collins wrote:
Victor Bazarov wrote:
>Ian Collins wrote:
>>JDT wrote:

NM_UPDOWN

With UDN_DELTAPOS notification handler, MS passes a pointer to
NMHDR which yo can cast to NM_UPDOWN*. With NMHDR (shown below) ,
you can use "iDelta" to know the amount it spins while with NMHDR
you can use "code" to know the event type. What I don't
understand is why the same storage (the overlap of the last int
and unsigned int) can provide two different values by casting. Is
it not union, is it? This is a C++ (or even a C) question. If I
underhand it, I may exploit the same tech for my own. Your advise
is appreciated. JD
It's not a technique, it's a horrible kludge.


[Mostly directed at the OP]

IOW, there are more reliable ways to do that in C++. However, in C,
since there is no inheritance, relying on certain relationship
between a pointer to a struct member and and the struct object itself
is not completely unheard of. Think "offsetof" macro.
It's still a horrible kludge. If you want to do such things in C,
use a union of structures with the first member of each struct being
the type identifier.
I don't see how it's better, and that's probably why I don't understand
the "horrible" attribute you used in your charcterisation.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 23 '07 #7
Victor Bazarov wrote:
Ian Collins wrote:
>>Victor Bazarov wrote:
>>>Ian Collins wrote:

JDT wrote:

>NM_UPDOWN
>
>With UDN_DELTAPOS notification handler, MS passes a pointer to
>NMHDR which yo can cast to NM_UPDOWN*. With NMHDR (shown below) ,
>you can use "iDelta" to know the amount it spins while with NMHDR
>you can use "code" to know the event type. What I don't
>understand is why the same storage (the overlap of the last int
>and unsigned int) can provide two different values by casting. Is
>it not union, is it? This is a C++ (or even a C) question. If I
>underhand it, I may exploit the same tech for my own. Your advise
>is appreciated. JD

It's not a technique, it's a horrible kludge.

[Mostly directed at the OP]

IOW, there are more reliable ways to do that in C++. However, in C,
since there is no inheritance, relying on certain relationship
between a pointer to a struct member and and the struct object itself
is not completely unheard of. Think "offsetof" macro.

It's still a horrible kludge. If you want to do such things in C,
use a union of structures with the first member of each struct being
the type identifier.

I don't see how it's better, and that's probably why I don't understand
the "horrible" attribute you used in your charcterisation.
Posting pre-coffee....

I should have written If you want to do such things in C, use a union of
structures with the first member of each struct and the union being the
type identifier, thus:

struct A {
int type;
/* Other members */
};

struct B {
int type;
/* Other members */
};

typedef union {
int type;
struct A a;
struct B b;
} Event;

It is then clear to the reader what an Event is and which types of event
are supported.

--
Ian Collins.
Apr 23 '07 #8

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

Similar topics

36
by: Bhalchandra Thatte | last post by:
I am allocating a block of memory using malloc. I want to use it to store a "header" structure followed by structs in my application. How to calculate the alignment without making any assumption...
47
by: sunglo | last post by:
Some time a go, in a discussion here in comp.lang.c, I learnt that it's better not to use a (sometype **) where a (void **) is expected (using a cast). Part of the discussion boiled down to the...
28
by: WaterWalk | last post by:
Hi, I'm haunted by 2 questions about struct copy. Though I searched the net, but still in confusion. 1. Does struct assignment copies every member including array members? For example, struct...
32
by: =?gb2312?B?zfWzrLey?= | last post by:
Union un { int I; char c; } main() { union un x; x.c=10; x.c=1;
5
by: wugon.net | last post by:
question: db2 LUW V8 UNION ALL with table function month() have bad query performance Env: db2 LUW V8 + FP14 Problem : We have history data from 2005/01/01 ~ 2007/05/xx in single big...
7
by: Chad | last post by:
#include <stdio.h> enum Type {DAYS, HOURSMINUTES}; struct Days { int num_days; }; struct HoursMinutes { int num_hours;
9
by: dspfun | last post by:
Hi! I have stumbled on the following C question and I am wondering wether the answer is implementation specific or if the answer is always valid, especially question b). BRs! ...
2
by: shenliang1985 | last post by:
I have Created two tables to test join key in sql server,but the result is not what I thougt.Why?Wish all your explains.Thanks.I give all the sql code in comment. --The table name is first for...
18
by: Stephan Beal | last post by:
Hi, all! Before i ask my question, i want to clarify that my question is not about the code i will show, but about what the C Standard says should happen. A week or so ago it occurred to me...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...
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
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,...
0
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...

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.