Why are the operators(+, -, * ,/) members?

Bernd Donner Bernd.Donner at gmx.net
Wed Nov 1 10:19:19 PST 2006


Hi


I'm just wondering if there is a specific reason why  operators(+,-,*,/) are 
members of wxSize. Usually, operator*, for instance, is written as a non 
member function like this:

wxSize operator*(const wxSize& sz, int i) {
  wxSize ret(sz);
  ret *= i;
  return ret;
}

This has two advantages compared to the current implementation:

1.) operator* is implemented in terms of operator*=

2.) We could also provide wxSize operator*(int i, const wxSize& sz). This is a
great advantage, I think.

The operators +, -, *, / are commonly implemented as non member functions (see 
for instance: http://doc.trolltech.com/4.2/qsize.html). The operators do not 
need access to private data, since they are implemented via operator*=.


Consider the already implemented case:

wxPoint wxPoint::operator+(const wxSize& s) const;

currently we may write: wxPoint+wxSize
but                                  wxSize+wxPoint
gives a syntax error since no overload is provided currently.

The above function operates on wxSize and on wxPoint. Putting it in wxPoint is 
somewhat arbitrary.

If there are no objections to this suggestion, I am willing to write a patch 
for gdicmn.h and gdicmn.cpp with the proposed changes.



Thanks,

Bernd




More information about the wx-dev mailing list