From -1477357142261960826
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: fc772,cda54862ecb4a41f
X-Google-Attributes: gidfc772,public
X-Google-Thread: f78e5,cda54862ecb4a41f
X-Google-Attributes: gidf78e5,public
X-Google-ArrivalTime: 2001-08-28 08:52:11 PST
Path: archiver1.google.com!newsfeed.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!cpk-news-hub1.bbnplanet.com!denver-snf1.gtei.net!news.gtei.net!namche.sun.com!news2me.EBay.Sun.COM!engnews1.eng.sun.com!taumet!clamage
From: Jack Klein <jackklein@spamcop.net>
Newsgroups: comp.std.c++,comp.lang.c++.moderated
Subject: Re: storage order of inherited classes
Date: 28 Aug 2001 15:36:57 GMT
Organization: AT&T Worldnet
Lines: 96
Approved: stephen.clamage@sun.com (comp.std.c++)
Message-ID: <5pqjot40to3q1rcrhokquq3e6k9s1sv2k3@4ax.com>
References: <MpMh7.6544$3f.1248650@news2-win.server.ntlworld.com>
NNTP-Posting-Host: taumet.eng.sun.com
X-NNTP-Posting-Host: netlab.cs.rpi.edu
X-Original-Date: Mon, 27 Aug 2001 06:42:26 GMT
X-Submission-Address: c++-submit@netlab.cs.rpi.edu
X-Auth: PGPMoose V1.1 PGP comp.lang.c++.moderated
	iQBVAwUAO4pj2EHMCo9UcraBAQEdvgH+IxPFM6IAwfV3d6B6KoigwD6AR2auPLfL
	8iu+8xzvqv0tAntWNI+5evkpLL3XTS5wMlULPR2tkM6HYwf9DBFw+w==
	=I3H2
X-Approved-For-Group: kuehl@fmi.uni-konstanz.de comp.lang.c++.moderated
Content-Length: 2394
X-Status: $$$$
X-UID: 0000000001
Originator: clamage@taumet
Xref: archiver1.google.com comp.std.c++:7211 comp.lang.c++.moderated:25536


On 25 Aug 2001 23:50:42 GMT, "Fray Bentos"
<LOVELYSPAMfraybentos@wonderfulspamphreaker.net> wrote in
comp.lang.c++.moderated:

> 
> Hey peeps.
> 
> Im involved in optimising a few C++ classes that interface with OpenGL
> I was wondering if the memory storage order of class members is
> standardised ?
> 
> Will the array j from the derived class B always be stored before i from the
> base class A ?

Your own code, undefined as it is, shows the array of ints in class A
being stored _before_ that from class B, not after.

> ------------------------8<---------------------
> #include <iostream>
> 
> class A
> {
> public:
> 	int i[4];
> 
> 	A(int a, int b, int c, int d) 
> 	{
> 		i[0] = a*10 ; i[1] = b*10;
> 		i[2] = c*10 ; i[3] = d*10;
> 	}
> };
> 
> class B : public A
> {
> public:
> 	int j[4];
> 
> 	B(int a, int b, int c, int d) : A(a, b, c, d)
> 	{
> 		j[0] = a; j[1] = b;
> 		j[2] = c; j[3] = d;
> 	}
> };
> 
> main()

I had to change this to a legal "int main()" to get this to compile
with my "better" compiler in strictly conforming mode.

> {
> 	B b(1, 2, 3, 4);
> 	int *ptr_b = reinterpret_cast<int*>(&b);
> 
> 	for(int i=0; i<sizeof(b) / sizeof(int); i++)
> 		std::cout << ptr_b[i] << std::endl;
> }
> ------------------------8<---------------------
> output:
> 10
> 20
> 30
> 40
> 1
> 2
> 3
> 4
> 
> Thanks

A base class appears before any of the additional data items added in
a derived class in a case like this.  Otherwise it would not be
possible for a pointer to a base class to point to a derived class
object.  The first sizeof(base class) bytes of the derived class must
be the instance of the base class in the derived class.

-- 
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq



      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]




