Innenleben von WxWidgets Classes



  • Mal ne Frage:

    wxPoint:

    class WXDLLEXPORT wxPoint
    {
    public:
        int x, y;
    
        wxPoint() : x(0), y(0) { }
        wxPoint(int xx, int yy) : x(xx), y(yy) { }
    
        // no copy ctor or assignment operator - the defaults are ok
    
        // comparison
        bool operator==(const wxPoint& p) const { return x == p.x && y == p.y; }
        bool operator!=(const wxPoint& p) const { return !(*this == p); }
    
        // arithmetic operations (component wise)
        wxPoint operator+(const wxPoint& p) const { return wxPoint(x + p.x, y + p.y); }
        wxPoint operator-(const wxPoint& p) const { return wxPoint(x - p.x, y - p.y); }
    
        wxPoint& operator+=(const wxPoint& p) { x += p.x; y += p.y; return *this; }
        wxPoint& operator-=(const wxPoint& p) { x -= p.x; y -= p.y; return *this; }
    
        wxPoint& operator+=(const wxSize& s) { x += s.GetWidth(); y += s.GetHeight(); return *this; }
        wxPoint& operator-=(const wxSize& s) { x -= s.GetWidth(); y -= s.GetHeight(); return *this; }
    
        wxPoint operator+(const wxSize& s) const { return wxPoint(x + s.GetWidth(), y + s.GetHeight()); }
        wxPoint operator-(const wxSize& s) const { return wxPoint(x - s.GetWidth(), y - s.GetHeight()); }
    };
    

    x und y sind hier "public" zugängliche Datenelemente.
    GetX() und GetY() existieren nicht.

    wxSize:

    class WXDLLEXPORT wxSize
    {
    public:
        // members are public for compatibility, don't use them directly.
        int x, y;
    
        // constructors
        wxSize() : x(0), y(0) { }
        wxSize(int xx, int yy) : x(xx), y(yy) { }
    
        // no copy ctor or assignment operator - the defaults are ok
    
        bool operator==(const wxSize& sz) const { return x == sz.x && y == sz.y; }
        bool operator!=(const wxSize& sz) const { return x != sz.x || y != sz.y; }
    
        wxSize operator+(const wxSize& sz) { return wxSize(x + sz.x, y + sz.y); }
        wxSize operator-(const wxSize& sz) { return wxSize(x - sz.x, y - sz.y); }
        wxSize operator/(const int i) { return wxSize(x / i, y / i); }
        wxSize operator*(const int i) { return wxSize(x * i, y * i); }
    
        wxSize& operator+=(const wxSize& sz) { x += sz.x; y += sz.y; return *this; }
        wxSize& operator-=(const wxSize& sz) { x -= sz.x; y -= sz.y; return *this; }
        wxSize& operator/=(const int i) { x /= i; y /= i; return *this; }
        wxSize& operator*=(const int i) { x *= i; y *= i; return *this; }
    
        void IncTo(const wxSize& sz)
            { if ( sz.x > x ) x = sz.x; if ( sz.y > y ) y = sz.y; }
        void DecTo(const wxSize& sz)
            { if ( sz.x < x ) x = sz.x; if ( sz.y < y ) y = sz.y; }
    
        // accessors
        void Set(int xx, int yy) { x = xx; y = yy; }
        void SetWidth(int w) { x = w; }
        void SetHeight(int h) { y = h; }
    
        int GetWidth() const { return x; }
        int GetHeight() const { return y; }
    
        bool IsFullySpecified() const { return x != wxDefaultCoord && y != wxDefaultCoord; }
    
        // combine this size with the other one replacing the default (i.e. equal
        // to wxDefaultCoord) components of this object with those of the other
        void SetDefaults(const wxSize& size)
        {
            if ( x == wxDefaultCoord )
                x = size.x;
            if ( y == wxDefaultCoord )
                y = size.y;
        }
    
        // compatibility
        int GetX() const { return x; }
        int GetY() const { return y; }
    };
    

    Hier sind x und y ebenfalls "public", aber man soll diese nicht verwenden 😉 , sondern brav GetWidth() bzw. GetX() und GetHeight() bzw. GetY() einsetzen.

    Ist das konsequent im Sinne von OOP und Datenkapselung?


  • Mod

    Nein, nicht direkt.
    Aber es steht ja auch da, das das nur für Kompabilität ist.
    wxWidgets ist schon recht alt, und schleppt deshalb sowas auch mit sich rum.
    Zum anderen ist es aber auch so, das bei einer Point klasse es durchaus
    üblich ist, das x und y public sind, und man sich die Setter/Getter schlicht spart.

    phlox



  • Bei Java ist es auch so, das x/y bei Point public sind. Auch bei Java Rectangle sind höhe und brreite public. wxWidgets ist da nicht alleine... die MFC machts auch so.



  • Was nicht heisst, dass es gut ist. Nicht war, Artchi?

    Allerdings ist die Verwendung von Public-Variablen in diesem Fall für mich Nachvollziehbar, solange die Argumentation sich auf Performance beschränkt. Bei Designs gilt es immer den optimalen Weg zwischen "so sollte mans machen" und Zweckmässigkeit zu finden. Das zeichnet einen qualifizierten Berufsmann aus.



  • Heute ist es wegen der Performance kein Thema in C++, die Compiler machen solche einfachen Getter und Setter als Inline. Wie es in Java ist, weiß ich nicht. Wird wahrscheinlich ein Performancegrund sein. Ich hab immer gedacht, es wurde in Java gemacht, um sich Tipp-Arbeit zu sparen und das es lesbarer ist.

    Das ganze Problem würde aber erst garnicht auftreten, wenn es Properties geben würde. :p C# hat es, in C++ gibts ja die propcpp. Allerdings hab ich letzteres noch nicht ausprobiert.


Anmelden zum Antworten