This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


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;


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]