Article View: alt.comp.lang.learn.c-c++
Article #773Re: Fast acces to class method
From: Brian Price
Date: Wed, 22 Mar 2000 00:00
Date: Wed, 22 Mar 2000 00:00
89 lines
3134 bytes
3134 bytes
In article <38D8E693.34731AA0@cyberdude.com>,
dataman@cyberdude.com wrote:
> What is the fastest way for accessing a member in a class.
>
> DATA is a structure;
>
> class A
> {
> public:
> //directly by having access to the member ?
> DATA data;
Bad practice... violates encapsulation.
>
> //by return value
> DATA GetData()
> {
> return data;
> }
>
Not good. Depending upon compiler and the definition of DATA you could
end up invoking multiple constructor calls and assignments. Might work
ok in simple cases but as a general rule it's bad practice. The
exception is in the case of a small (2 ints or less) structure or class
that has no implicit constructor, destructor, or assignment operator.
> //by reference
> void GetData(DATA& d)
> {
> d=data;
> }
>
Best by far. Most compilers will turn that into inline code. Better
is to declare the method in the body and then implement as inline using:
// in body:
void GetData( DATA & d ) const;
// outside of body but still in header:
inline void A:GetData( DATA & d ) const
{
d = data;
}
> //by pointer
> void GetData(DATA* pD)
> {
> *pData=data;
> }
The same as by reference with the following exception: Bad practice,
very bad practice. Only use pointers when you are transferring
ownership of the object, otherwise in more complex scenarios you will
rapidly lose track of who owns what and object lifetime issues will
haunt you.
> }
>
> I am programing a real time application (a 3D game) , that access very
> often the DATA (several times per second) in the class A .
> So, i need the fastest access to it , even if the OOP may suffer (like
> accessing directly DATA without a method) .
>
You can be as fast as C (or even assembler) without violating OOP
principles when using C++ (with inline functions and inline assembler).
> You can give me the + and - of every method (speed , memory ..etc.) ,
so
> i can have a best look.
>
> Thank you.
>
> --
> The Aero Section : http://fly.to/TheAeroSection/
> The Aero Section is a large military aircraft photo gallery.
>
>
Hope this helps,
--
Brian L. Price
Have compiler will travel.
Sent via Deja.com http://www.deja.com/
Before you buy.
Technical Details
Message-ID:
<8basal$5vd$1@nnrp1.deja.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<38D8E693.34731AA0@cyberdude.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 109fba,d558f056865fc0f1 X-Google-Attributes: gid109fba,public X-Google-Thread: 114592,d558f056865fc0f1 X-Google-Attributes: gid114592,public X-Google-Thread: fdcad,d558f056865fc0f1 X-Google-Attributes: gidfdcad,public From: Brian Price <brianleeprice@hotmail.com> Subject: Re: Fast acces to class method Date: 2000/03/22 Message-ID: <8basal$5vd$1@nnrp1.deja.com> X-Deja-AN: 600896136 References: <38D8E693.34731AA0@cyberdude.com> X-Http-Proxy: 1.0 x32.deja.com:80 (Squid/1.1.22) for client 4.54.61.81 Organization: Deja.com - Before you buy. X-Article-Creation-Date: Wed Mar 22 16:28:28 2000 GMT X-MyDeja-Info: XMYDJUIDbrianleeprice Newsgroups: alt.comp.lang.learn.c-c++,comp.lang.c++,fr.comp.lang.c++ X-Http-User-Agent: Mozilla/4.0 (compatible; MSIE 5.0; Windows 98; DigExt) Proxy/4.02 (www.analogx.com)