Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von »anti-freak« (09.08.2014, 13:03)
C-/C++-Quelltext |
|
1 2 3 4 5 |
class Derived : public Base { public: // for each operator: Derived& operator+(const Derived& o) { Base::operator+(static_cast<const Base&>(o)); return *this; } }; |
C-/C++-Quelltext |
|
1 2 |
struct Foo{int a;}; class Bar : public Foo {int a;}; |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 |
struct Foo{int a;}; struct Bar : public Foo {int a; int& getFooA(){return Foo::a;};}; //later Bar hi; hi.a = 4; hi.getA() = 6; std::cout << hi.a << " " << hi.getFooA(); //stdout:4 6 |
"Basisklasse und Childklassen haben identische Variabeln"?
Meinst du so etwas:
C-/C++-Quelltext
1 2 struct Foo{int a;}; class Bar : public Foo {int a;};
Die Membervariablen sind die selben und die Funktionsweise der Operatoren soll sich nicht ändern?
Seltsam, aber was du machen kannst, ist die Operators der vererbenden Klasse zu überladen und darin die der Base-Klasse aufzurufen:
C-/C++-Quelltext
1 2 3 4 5 class Derived : public Base { public: // for each operator: Derived& operator+(const Derived& o) { Base::operator+(static_cast<const Base&>(o)); return *this; } };
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
class Foo { private: int x; public: void setX(int x) //... int getX() const //... } class Bar : public Foo { } template <class T> T operator+(const T& lhs, const Bar& rhs) { auto tmp = lhs; tmp.setX(lhs.getX() + rhs.getX()); return tmp; } template <class T> T& operator+=(const T& lhs, const Bar& rhs) { lhs.setX(lhs.getX() + rhs.getX()); return lhs; } |
Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von »anti-freak« (09.08.2014, 14:21)
Zitat
Wieso denn seltsam?
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class Salt { private: int m_AmountInGramm; public: Salt& operator+(const Salt& rhs) { m_AmountInGramm += rhs.m_AmountInGramm; return *this; } }; class RoadSalt : public Salt { public: void PutOnRoad(const Road& r) { r.PutSaltOnIt(*this); } // Notice the forwarding = bad design! }; class PotatoSalt : public Salt { public: void PutOnPotato(const Potato& p) { ... } }; |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 |
struct ISaltable{ virtual void PutSaltOnit(const Salt& salt) = 0; }; class Road : public ISaltable { public: virtual void PutSaltOnIt(const Salt& salt) { ... } }; class Potato : public ISaltable { virtual void PutSaltOnIt(const Salt& salt) { ... } }; |
Dieser Beitrag wurde bereits 3 mal editiert, zuletzt von »iSmokiieZz« (09.08.2014, 14:36)
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
template<typename T> class PointBase { public: PointBase(T X, T Y) : m_X(X), m_Y(Y) { } T getX() const { return m_X; } T getY() const { return m_Y; } T& getX() { return m_X; } T& getY() { return m_Y; } private: T m_X; T m_Y; }; |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
class Point : public PointBase<int32> { public: Point(int32 x = 0, int32 y = 0) : PointBase<int32>(x, y) { } Point& operator +=(const Point& p) { getX() += p.getX(); getY() += p.getY(); return *this; } Point& operator -=(const Point& p); Point& operator *=(const Point& p); Point& operator /=(const Point& p); Point& operator +=(const int32 &t); Point& operator -=(const int32 &t); Point& operator *=(const int32 &t); Point& operator /=(const int32 &t); }; Point operator +(Point const& lhs, const Point& rhs) { Point tmp(lhs); tmp += rhs; return tmp; } Point operator -(Point const& lhs, const Point& rhs); Point operator *(Point const& lhs, const Point& rhs); Point operator /(Point const& lhs, const Point& rhs); Point operator +(Point const& lhs, int32 rhs); Point operator -(Point const& lhs, int32 rhs); Point operator *(Point const& lhs, int32 rhs); Point operator /(Point const& lhs, int32 rhs); |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 |
template<typename T> class PointBase { private: T m_X, m_Y; public: PointBase<T>& operator+(const PointBase<T>& rhs) { m_X += rhs; m_Y += rhs; return *this; } // ... }; typedef PointBase<int> IntPoint; |
C-/C++-Quelltext |
|
1 2 3 4 |
template<class T,int SIZE> struct Point { T data[SIZE]; } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
template<class T> struct Point<T,2> { union { T data[2]; struct { T x,y; } } Point<T,2>() : x(0) , y(0) {} // mehr Konstruktoren // und noch ein bisschen was const T& operator[] (int idx) const { return data[idx];} T* operator() () { return &data[0]; } //! assignment operator Point<T,2>& operator = (const Point<T,2>& other) { x = other.x; y = other.y; return *this; } } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 |
template<class T,int SIZE> bool operator == (const Point<T,SIZE>& u,const Point<T,SIZE>& v) { for ( int i = 0; i < SIZE; ++i ) { if ( u.data[i] != v.data[i] ) { return false; } } return true; } |
C-/C++-Quelltext |
|
1 2 |
typedef Point<float,2> Point2f; typedef Point<float,3> Point3f; |
Zitat
PointF (auch eine der Childklassen) z.B. hat noch ein paar andere Methoden, die nichts in PointBase zu suchen haben.
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
template <typename T> class PointBase { private: T m_X, m_Y; public: PointBase() { } PointBase(const PointBase<T>& o) : m_X(o.m_X), m_Y(o.m_Y) { } PointBase(const T& x, const T& y) { m_X = x; m_Y = y; } PointBase<T>& operator+(const PointBase<T>& rhs) { m_X += rhs.m_X; m_Y += rhs.m_Y; return *this; } }; class PointF : public PointBase<float> { public: PointF() : PointBase<float>() { } PointF(const PointBase<float>& baseF) : PointBase<float>(baseF) { } void DoSomething() { } }; PointF p1, p2; p1 = p1 + p2; |
Werbeanzeige