Thread View: comp.sys.mac.programmer.codewarrior
3 messages
3 total messages
Started by Michael
Thu, 07 Aug 2003 23:12
Preserving class member-variable order?
Author: Michael
Date: Thu, 07 Aug 2003 23:12
Date: Thu, 07 Aug 2003 23:12
31 lines
2285 bytes
2285 bytes
Hi,
I'm dev'ing in C++ using Codewarrior 8.3, on OS 9.2 & X.
I'm trying to find out if there's a way to preserve the order of class
member variables, ie the order in which they are listed in the class
declaration. I'm hoping to be able to reference a class member indirectly by
deref'ing another class member that is a known distance away. I've done this
before on Windows (VC++) and it works nicely, but the same approach doesn't
seem to work in OS 9 (or maybe because of the CW compiler?). I haven't tried
yet on OS X but I imagine it might be the same on OS 9?
A simple example is below. This simple example might work, but I've been
trying to get it to work in a much larger class. I haven't tested
extensively, thinking that someone might have a quick answer for me.
class MyClass{
char interestingValue;
char placeHolder;
}
MyClass obj;
char val = *(&(obj.placeHolder)-1);
I was thinking there might be a compiler option or pragma I could use to fix
the order of the members during compilation. Does anyone have any
suggestions?
Many thanks,
Michael
Re: Preserving class member-variable order?
Author: John Harrison
Date: Fri, 08 Aug 2003 08:18
Date: Fri, 08 Aug 2003 08:18
52 lines
2649 bytes
2649 bytes
"Michael" <mstauff@bellatlantic.net> wrote in message
news:r1BYa.1145$014.1012@nwrdny02.gnilink.net...
> Hi,
>
> I'm dev'ing in C++ using Codewarrior 8.3, on OS 9.2 & X.
>
> I'm trying to find out if there's a way to preserve the order of class
> member variables, ie the order in which they are listed in the class
> declaration. I'm hoping to be able to reference a class member indirectly
by
> deref'ing another class member that is a known distance away. I've done
this
> before on Windows (VC++) and it works nicely, but the same approach
doesn't
> seem to work in OS 9 (or maybe because of the CW compiler?). I haven't
tried
> yet on OS X but I imagine it might be the same on OS 9?
>
> A simple example is below. This simple example might work, but I've been
> trying to get it to work in a much larger class. I haven't tested
> extensively, thinking that someone might have a quick answer for me.
>
> class MyClass{
> char interestingValue;
> char placeHolder;
> }
>
> MyClass obj;
>
> char val = *(&(obj.placeHolder)-1);
>
> I was thinking there might be a compiler option or pragma I could use to
fix
> the order of the members during compilation. Does anyone have any
> suggestions?
>
> Many thanks,
> Michael
>
I don't think you need any pragmas or compiler options.
#include <stddef.h>
char val = *(&(obj.placeHolder) - (offsetof(MyClass, placeHolder) -
offsetof(MyClass, interestingValue)));
offsetof is a macro defined in <stddef.h>.There are some restrictions on
what sort of class you can use offsetof with.
john
Re: Preserving class member-variable order?
Author: Michael
Date: Mon, 11 Aug 2003 18:40
Date: Mon, 11 Aug 2003 18:40
61 lines
3129 bytes
3129 bytes
"John Harrison" <john_andronicus@hotmail.com> wrote in message
news:bgvm8q$smqin$1@ID-196037.news.uni-berlin.de...
>
> "Michael" <mstauff@bellatlantic.net> wrote in message
> news:r1BYa.1145$014.1012@nwrdny02.gnilink.net...
> > Hi,
> >
> > I'm dev'ing in C++ using Codewarrior 8.3, on OS 9.2 & X.
> >
> > I'm trying to find out if there's a way to preserve the order of class
> > member variables, ie the order in which they are listed in the class
> > declaration. I'm hoping to be able to reference a class member
indirectly
> by
> > deref'ing another class member that is a known distance away. I've done
> this
> > before on Windows (VC++) and it works nicely, but the same approach
> doesn't
> > seem to work in OS 9 (or maybe because of the CW compiler?). I haven't
> tried
> > yet on OS X but I imagine it might be the same on OS 9?
> >
> > A simple example is below. This simple example might work, but I've been
> > trying to get it to work in a much larger class. I haven't tested
> > extensively, thinking that someone might have a quick answer for me.
> >
> > class MyClass{
> > char interestingValue;
> > char placeHolder;
> > }
> >
> > MyClass obj;
> >
> > char val = *(&(obj.placeHolder)-1);
> >
> > I was thinking there might be a compiler option or pragma I could use to
> fix
> > the order of the members during compilation. Does anyone have any
> > suggestions?
> >
> > Many thanks,
> > Michael
> >
>
> I don't think you need any pragmas or compiler options.
>
> #include <stddef.h>
>
> char val = *(&(obj.placeHolder) - (offsetof(MyClass, placeHolder) -
> offsetof(MyClass, interestingValue)));
>
> offsetof is a macro defined in <stddef.h>.There are some restrictions on
> what sort of class you can use offsetof with.
>
> john
Thanks John! I'll look into that.
Cheers,
Michael
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads