tutorials.de Buch-Aktion 05/2012
ERLEDIGT
JA
ANTWORTEN
3
ZUGRIFFE
1809
EMPFEHLEN
  • An Twitter übertragen
  • An Facebook übertragen
AUF DIESES THEMA
ANTWORTEN
  1. #1
    FireFlow FireFlow ist offline Mitglied Gold
    Registriert seit
    Feb 2004
    Beiträge
    213
    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);
    }
     
    --//--//--// My StillLife! //--//--//--
    Obere URL dient nur als Google-Push )

  2. #2
    Avatar von Endurion
    Endurion Endurion ist offline Mitglied Diamant
    Registriert seit
    Apr 2004
    Beiträge
    2.151
    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.
     

  3. #3
    FireFlow FireFlow ist offline Mitglied Gold
    Registriert seit
    Feb 2004
    Beiträge
    213
    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?
     
    --//--//--// My StillLife! //--//--//--
    Obere URL dient nur als Google-Push )

  4. #4
    Avatar von Endurion
    Endurion Endurion ist offline Mitglied Diamant
    Registriert seit
    Apr 2004
    Beiträge
    2.151
    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

  1. const void *
    Von ubok im Forum VisualStudio & MFC
    Antworten: 2
    Letzter Beitrag: 02.07.10, 12:18
  2. const int und pow(x,y)
    Von Ozzy Ozborn im Forum C/C++
    Antworten: 3
    Letzter Beitrag: 13.06.07, 21:44
  3. Int in const char*
    Von Sircoly im Forum C/C++
    Antworten: 3
    Letzter Beitrag: 15.07.06, 19:40
  4. const vor dem Funktion
    Von Don_Pazo im Forum C/C++
    Antworten: 1
    Letzter Beitrag: 22.01.06, 00:39
  5. C: const Pointer
    Von Konsumierer im Forum C/C++
    Antworten: 7
    Letzter Beitrag: 30.06.05, 15:03