🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: alt.comp.lang.learn.c-c++
Article #777

Re: Fast acces to class method

#777
From: Daniel T.
Date: Wed, 22 Mar 2000 00:00
54 lines
2034 bytes
In article <38D8E693.34731AA0@cyberdude.com>, dataman@cyberdude.com wrote:

>What is the fastest way for  accessing a member  in  a class.

struct DATA

class A {
public:
    const DATA& get_data() const          { return data; }
    void        set_data( const DATA& d ) { data = d; }
private:
    DATA data;
};

(Some people don't like actually having the words 'get' and 'set' in the
methods. I like them because it makes things much easer when I'm doing a
global search for all the places where data is changed.)

If you find yourself diving into a chain of class/structs, then apply a
little of the law of demeter. IE

if you have:

struct DATA {
    int x;
    int y;
};

And you are doing a lot of:

A myA;
myA.get_data().x;
myA.get_data().y;

Then you should write A like this:

class A {
public:
    int get_x() const { return data.x; }
    int get_y() const { return data.y; }
private:
    DATA data;
};

The above is every bit as fast as:

struct DATA {
    int x;
    int y;
};

struct A {
    DATA data;
};
Technical Details
Message-ID: <daniel_t-2203001439380001@200.0.0.81>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <38D8E693.34731AA0@cyberdude.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 114592,d558f056865fc0f1
X-Google-Attributes: gid114592,public
X-Google-Thread: 109fba,d558f056865fc0f1
X-Google-Attributes: gid109fba,public
X-Google-Thread: fdcad,d558f056865fc0f1
X-Google-Attributes: gidfdcad,public
From: daniel_t@gorilla.com (Daniel T.)
Subject: Re: Fast acces to class method
Date: 2000/03/22
Message-ID: <daniel_t-2203001439380001@200.0.0.81>
X-Deja-AN: 600982320
Distribution: world
References: <38D8E693.34731AA0@cyberdude.com>
X-Abuse-Info: Otherwise we will be unable to process your complaint properly
X-Complaints-To: abuse@gte.net
X-Trace: +4awYTHP4azr5esPBAtayM5Iiyco2q/rHoaz1NTNlXyPErjiOhHSoJEKdBKtEmCKvX5z8jPTTA/9!GDE3WBJTzkvWwDYL2NUzUJ7NNQu3T0lUSESZNYbZkcVaszViREgJoTLBiBbxX58QC2blV5MzX+e6!XQ==
Organization: Gorilla Systems Corp.
NNTP-Posting-Date: Wed, 22 Mar 2000 19:39:39 GMT
Newsgroups: alt.comp.lang.learn.c-c++,comp.lang.c++,fr.comp.lang.c++