🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.sys.mac.programmer.codewarrior
Article #715

Re: Preserving class member-variable order?

#715
From: John Harrison
Date: Fri, 08 Aug 2003 08:18
52 lines
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
Technical Details
Message-ID: <bgvm8q$smqin$1@ID-196037.news.uni-berlin.de>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!bloom-beacon.mit.edu!nycmny1-snh1.gtei.net!news.gtei.net!newsfeed.mathworks.com!fu-berlin.de!uni-berlin.de!80.234.164.35!not-for-mail
References: <r1BYa.1145$014.1012@nwrdny02.gnilink.net>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10bcce,e09790fdeb4be6df
X-Google-Attributes: gid10bcce,public
X-Google-Thread: f635e,e09790fdeb4be6df
X-Google-Attributes: gidf635e,public
X-Google-ArrivalTime: 2003-08-08 01:16:28 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!bloom-beacon.mit.edu!nycmny1-snh1.gtei.net!news.gtei.net!newsfeed.mathworks.com!fu-berlin.de!uni-berlin.de!80.234.164.35!not-for-mail
From: "John Harrison" <john_andronicus@hotmail.com>
Newsgroups: comp.sys.mac.programmer.codewarrior,codewarrior.mac
Subject: Re: Preserving class member-variable order?
Date: Fri, 8 Aug 2003 09:18:54 +0100
Lines: 54
Message-ID: <bgvm8q$smqin$1@ID-196037.news.uni-berlin.de>
References: <r1BYa.1145$014.1012@nwrdny02.gnilink.net>
NNTP-Posting-Host: 80.234.164.35
X-Trace: news.uni-berlin.de 1060330587 30108247 80.234.164.35 (16 [196037])
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106