Boost logo

Boost :

From: Dave Harris (brangdon_at_[hidden])
Date: 2007-10-07 08:32:03


In-Reply-To: <1191527843170_at_[hidden]>
spam_from_boost_dev_at_[hidden] (Phil Endecott) wrote (abridged):
> > http://www.gamedev.net/community/forums/topic.asp?topic_id=261920
>
> Yes, a very good page showing several solutions:
>
> 1. A vector of member pointers (may have some overhead).
> 2. Anonymous structs and unions (may be non-standard).
> 3. x, y and z implemented as references (makes object bigger).
> 4. Relying on (&x)[1] pointing to y.

It misses this one:

    template <typename T>
    class Point3 {
        T v[3];
    public:
         T &operator[]( size_t i ) { return v[i]; }
         T &x() { return v[0]; }
         T &y() { return v[1]; }
         T &z() { return v[2]; }
    };

which in my view is the only way to fly. It has no run-time overhead
(provided the trivial functions are inlined). It does not rely on
non-standard or non-portable features. It's simple and easy to understand.
It has the added benefit that the underlying representation is kept
private, and all access done via member functions.

The main drawback is the extra brackets.
    cout << p.x();

rather than:
    cout << p.x;

Which is a pain, but I think the benefits are worth it.

-- Dave Harris, Nottingham, UK.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk