Article View: comp.sys.mac.programmer.codewarrior
Article #724Re: Preserving class member-variable order?
From: 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
Technical Details
Message-ID:
<KqRZa.154$jw4.133@nwrdny03.gnilink.net>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!headwall.stanford.edu!newshub.sdsu.edu!elnk-nf2-pas!newsfeed.earthlink.net!wn14feed!worldnet.att.net!199.45.49.37!cyclone1.gnilink.net!spamkiller2.gnilink.net!nwrdny03.gnilink.net.POSTED!53ab2750!not-for-mailReferences:
<r1BYa.1145$014.1012@nwrdny02.gnilink.net> <bgvm8q$smqin$1@ID-196037.news.uni-berlin.de>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f635e,e09790fdeb4be6df X-Google-Attributes: gidf635e,public X-Google-Thread: 10bcce,e09790fdeb4be6df X-Google-Attributes: gid10bcce,public X-Google-ArrivalTime: 2003-08-11 11:40:42 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!headwall.stanford.edu!newshub.sdsu.edu!elnk-nf2-pas!newsfeed.earthlink.net!wn14feed!worldnet.att.net!199.45.49.37!cyclone1.gnilink.net!spamkiller2.gnilink.net!nwrdny03.gnilink.net.POSTED!53ab2750!not-for-mail From: "Michael" <mstauff@bellatlantic.net> Newsgroups: comp.sys.mac.programmer.codewarrior,codewarrior.mac References: <r1BYa.1145$014.1012@nwrdny02.gnilink.net> <bgvm8q$smqin$1@ID-196037.news.uni-berlin.de> Subject: Re: Preserving class member-variable order? Lines: 63 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4920.2300 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4920.2300 Message-ID: <KqRZa.154$jw4.133@nwrdny03.gnilink.net> Date: Mon, 11 Aug 2003 18:40:42 GMT NNTP-Posting-Host: 151.197.16.150 X-Complaints-To: abuse@verizon.net X-Trace: nwrdny03.gnilink.net 1060627242 151.197.16.150 (Mon, 11 Aug 2003 14:40:42 EDT) NNTP-Posting-Date: Mon, 11 Aug 2003 14:40:42 EDT