I'm sorry, but this is unacceptable (union members and ctors)

Antoine Chavasse a.chavasse@gmail.com
Wed Jun 27 17:47:00 GMT 2007


> For instance, say you need to impliment a GUI, so you have yourself a
> rectangle struct which consists of four floating point values (the origin
> and difference between the opposite corner) ...Now you want those four
> values, but you also have a 2D vector struct.

Here is a portable alternative to achieve this:

struct Rectangle
{
  private:
    Vector2D m_Position;
    Vector2D m_Size;

  public:
    Vector2D& position() { return m_Position; }
    const Vector2D& position() const { return m_Position; }

    Vector2D& size() { return m_Size; }
    const Vector2D& size() const { return m_Size; }

    float& left() { return m_Position.x; }
    float left() const { return m_Position.x; }

    float& top() { return m_Position.y; }
    float top() const { return m_Position.y; }

    float& width() { return m_Size.x; }
    float width() const { return m_Size.x; }

    float& height() { return m_Size.y; }
    float height() const { return m_Size.y; }
};

Then you can access the members like this:

Rect somerectangle;

Rect.position().x = 45;
Rect.left() = 45;



More information about the Gcc mailing list