[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-devel
Subject: Re: casting an 'int' to a class
From: Ellis Whitehead <kde () ellisw ! net>
Date: 2002-01-22 3:30:29
[Download RAW message or body]
> 1) remove the &
> 2) I *think* some platforms (sparc64?) have sizeof(int) != sizeof(void*)
>
> Of course, if sizeof(int) and sizeof(void*) were the same, and the variable
> is private, you should be able to just change the type.
I don't really follow you... What's this about a void*? I'm not wanting to
point to anything, but rather use an 'int' as though it were a particular
class which contains a single 'int' member.
So if I say:
int b = 123;
cout << ((A*)&b)->a << endl;
it prints 123.
So what I need to know is that all systems treat a struct { int a; } value the
same a an 'int' in memory?
On Monday 21 January 2002 21:57, Charles Samuels wrote:
> On Monday 21 January 2002 04:54 pm, Ellis Whitehead wrote:
> > Would the following cast from an int to a struct cause problems on any
> > systems?
> >
> > struct A {
> > int a;
> > int x();
> > };
> >
> > int foo() {
> > int b = 0;
> > return ((A*)(&b))->x();
> > }
>
> Eeewwww :)
>
> > I have a couple classes related to kdelibs/kdecore/kkey* that contain
> > only a single 'int' of data. I would like to keep them private and put
> > 'int's in the containing class definition.
>
> 1) remove the &
> 2) I *think* some platforms (sparc64?) have sizeof(int) != sizeof(void*)
>
> Of course, if sizeof(int) and sizeof(void*) were the same, and the variable
> is private, you should be able to just change the type.
>
> > Thanks,
> > Ellis
> >
> > >> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to
> > >> unsubscribe <<
> >>
> >> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to
> >> unsubscribe <<
>> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe <<
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic