ERLEDIGT
JA
JA
ANTWORTEN
3
3
ZUGRIFFE
1809
1809
EMPFEHLEN
-
Ich habe eine Klasse die eine Bruchzahl speichert (naja bis nun noch nichts tolles
).
Ich hab in der Klasse eine konstante öffentliche Methode bool const isGreater(rational const& a). Wenn ich nun von meiner Vergleichsoperatorfunktion bool const operator > (rational const& a, rational const& b) auf die Methode isGreater von der Instanz a zugreife spuckt mir der Compiler folgende Warnung aus:
[C++ Warning] classRational.cpp(242): W8037 Non-const function rational::isGreater(const rational &) called for const object
Hier der Code:
Code :1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
class rational { public: // [...] // Vergleiche bool const isGreater(const rational& a); // [...] private: // [...] }; bool const rational::isGreater(const rational& a) { return (double)this->zaehler/this->nenner > (double)a.zaehler/a.nenner; }
Code :1 2 3 4
bool const operator > (rational const& a, rational const& b) { return a.isGreater(b); }
-
Müsste da nicht hinter der Funktion noch ein const kommen:
bool const rational::isGreater(const rational& a) const
Das erste const sagt aus, dass das zurückgegebene bool const ist, das zweite vor rational, dass a nicht verändert wird. Und das const hintendran, dass das betroffene Objekt nicht verändert wird.
-
Danke daran lags ich hab das irgendwie verwechselt und dachte das const davor wär dass die Methode konstant wird. Aber ich verstehe noch nicht ganz wie eine Funktion einen konstanten Wert zurückgeben kann...? Welchen Sinn hat das?
-
Das bedeutet, dass man das bool nicht verändern darf/kann. Macht bei integrierten Typen nicht so viel Sinn, eher bei Klassen.
Gewisse Kachelatoren können dir bestimmt 1 bis 75 Beispiele nennen
Ähnliche Themen
-
const void *
Von ubok im Forum VisualStudio & MFCAntworten: 2Letzter Beitrag: 02.07.10, 12:18 -
const int und pow(x,y)
Von Ozzy Ozborn im Forum C/C++Antworten: 3Letzter Beitrag: 13.06.07, 21:44 -
Int in const char*
Von Sircoly im Forum C/C++Antworten: 3Letzter Beitrag: 15.07.06, 19:40 -
const vor dem Funktion
Von Don_Pazo im Forum C/C++Antworten: 1Letzter Beitrag: 22.01.06, 00:39 -
C: const Pointer
Von Konsumierer im Forum C/C++Antworten: 7Letzter Beitrag: 30.06.05, 15:03





Zitieren
Login






