Hi,
ich verstehe da was nicht. Warum funktioniert diese Operatorüberladung in Postfix, hier die Klassendeklaration:
Und hier die Definition:
Ich nutze den Standardkopierkonstruktor um den Inhalt des This Zeigers in die lokale Variable tmp zu kopieren. Dann erhöhe ich mit dem Präfixoperator ++ die beiden Eigenschaften, das sind die des this-Zeigers. Und gebe tmp zurück ... an tmp habe ich doch nichts geändert, warum funktioniert das so? Gruß, kbs
ich verstehe da was nicht. Warum funktioniert diese Operatorüberladung in Postfix, hier die Klassendeklaration:
Klassendeklaration myComplex:
#ifndef myCOMPLEX_H
#define myCOMPLEX_H
#include <iostream>
class myComplex {
private:
int imag;
int real;
public:
myComplex();
myComplex(int, int);
myComplex &operator++();
myComplex operator++(int);
};
#endif
Und hier die Definition:
Klassendefinition myComplex:
#include "myComplex.h"
#include <iostream>
myComplex::myComplex() {
imag = 0;
real = 0;
}
myComplex::myComplex(int z1, int z2) : real(z1), imag(z2) {}
myComplex &myComplex::operator++() {
++real;
++imag;
return *this;
}
myComplex myComplex::operator++(int) {
myComplex tmp(*this);
++imag;
++real;
return tmp;
}
Ich nutze den Standardkopierkonstruktor um den Inhalt des This Zeigers in die lokale Variable tmp zu kopieren. Dann erhöhe ich mit dem Präfixoperator ++ die beiden Eigenschaften, das sind die des this-Zeigers. Und gebe tmp zurück ... an tmp habe ich doch nichts geändert, warum funktioniert das so? Gruß, kbs