From 2358430241455197841
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,cda54862ecb4a41f
X-Google-Attributes: gidf78e5,public
X-Google-Thread: fc772,cda54862ecb4a41f
X-Google-Attributes: gidfc772,public
X-Google-ArrivalTime: 2001-08-27 08:02:18 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: "Justin Randall" <jrandall@verant.com>
Newsgroups: comp.std.c++,comp.lang.c++.moderated
Subject: Re: storage order of inherited classes
Date: 27 Aug 2001 14:55:43 GMT
Organization: unknown
Lines: 107
Approved: stephen.clamage@sun.com (comp.std.c++)
Message-ID: <3b8a4db7$0$26333@news.denver1.Level3.net>
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 08:40:32 -0500
X-Submission-Address: c++-submit@netlab.cs.rpi.edu
X-Auth: PGPMoose V1.1 PGP comp.lang.c++.moderated
	iQBVAwUAO4pcf0HMCo9UcraBAQGzfQH/d1MHjNiUoOObfjFhv3Mr06FfGZVZptGl
	B2m9+nY5vJ1QbwAdl8QNHAScK3SaK3gHZA3gZe6l8RNJkbfqBqYSbw==
	=GO0E
X-Approved-For-Group: kuehl@fmi.uni-konstanz.de comp.lang.c++.moderated
Content-Length: 2805
X-Status: $$$$
X-UID: 0000000001
Originator: clamage@taumet
Xref: archiver1.google.com comp.std.c++:7177 comp.lang.c++.moderated:25426


"Fray Bentos" <LOVELYSPAMfraybentos@wonderfulspamphreaker.net> wrote in
message news:MpMh7.6544$3f.1248650@news2-win.server.ntlworld.com...
>
> 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 ?
>

It depends on how it is inherited. Storage order with virtual inheritance
differs. It's usually safer, cleaner and more correct to avoid direct memory
access, and use inlined accessor functions. In your example, you could
define operator[] if you really need to access a class as if it were an
array.

Even if the classes aren't currently using any elaborate inheritence
mechanisms right now, that doesn't mean that they won't in the future.

E.g.

class A
{
    int a;
};

class B : public class A
{
    int b;
};

class C
{
    int c;
}

what now?
class D : public class C, public class B
{
    double d;
};

** or **
class D : public class B, public class C
{
    double d;
};

I am assuming that your classes are really doing more than storing data. If
you are inheriting, what do you do about your vtable? If you don't declare
your destructors virtual, your instances will not be properly destroyed.

what happens if an instance of class D is referred to as a class A with C
members at the front? Your old code behaves unexpectedly simply because
someone uses a feature of the language to extend your old classes.

Instead, try:
class A
{
public:
    A();
    virtual ~A();
    int operator[] (const int index) const;
private:
    int *a;
    int allocatedSize;
};

inline int A::operator[](const int index) const
{
    assert(index < allocatedSize );
    return a[index];
}

now, this would work no matter what the inheritence mechanism is. You will
always access A's members properly and even be able to validate your
accessors at runtime in debug mode.

void foo()
{
    D d;
    int j = d[3];
}

If you are concerned about performance, try testing the direct memory access
method vs using safer accessors. I've found that even for 3D rendering, the
cost is negligable compared to other operations (matrix operations, bus
transfer of texture and polygon data, etc..)




      [ 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.     ]




