Operatorüberladung



  • hab ein Problem mit der Operatorüberladung und ich weiß nicht was ich falsch mache. Ich mache es exact wie in meinem Buch.

    testclass::testclass(int A, double B=5.0, float C=2.0):a(A),b(B),c(C)/{iCounter++;}// Konstruktor
    
    testclass testclass::operator+(testclass obj)
    {
    testclass help;
    help.a = a + obj.a;
    help.b = b + obj.b;
    help.c = c + obj.c;
    return help;
    }
    
    void testclass::print()
    {
    cout<<a<<" "<<b<<" "<<c<<endl;
    }
    

    also schlichtweg alle Membervariablen der Objekte addieren.
    dann weiter unten in der main Funktion

    testclass objekt1(10,15);
    testclass objekt2(10,15);
    testclass temp = objekt1.operator+(objekt2)// objekt1+objekt2;
    temp.print
    

    und anstatt daß die Ausgabe 20,30,4 ist bekomme ich immer irgendwelchen Speichermüll.

    😕 😕 😕 😕 😕



  • temp.print();

    muss da noch hin nicht nur temp.print aber



  • Der Code sieht eigentlich gut aus. Kannst Du mal das komplette Beispiel posten (also praktisch ne lauffähige Version)?



  • #include <iostream>
    
    using namespace std;
    
    class testclass
    {
    private:
            static int iCounter;
            int a;
            double b;
            float c;
    public:
            testclass (int A, double B = 5.0, float C = 2.0F);
                    // Defaultparameter sollten bei der Deklaration angegeben werden!
            ~testclass (void);
    
            testclass operator + (const testclass& obj);    // <-- anstatt testclass obj,
                                                            //dann muß das Objekt nicht kopiert werden
            void print (void);
    };
    
    int testclass::iCounter = 0;
    
    testclass::testclass(int A, double B, float C)
     : a(A),b(B),c(C)
    {
            ++iCounter;
    }
    
    testclass::~testclass (void)
    {
            --iCounter;
    }
    
    testclass testclass::operator + (const testclass& obj) // <-- anstatt testclass obj
    {
            /*testclass help;
            help.a = a + obj.a;
            help.b = b + obj.b;
            help.c = c + obj.c;
            return help;*/ // Verfahren hier zu langsam, so ist es schneller:
    
            return (testclass (a + obj.a, b + obj.b, c + obj.c));
            // dadurch sparst du
            // - Erstellungszeit für help
            // - Zeit für unnötigen Defaultkonstruktor
    }
    
    void testclass::print (void)
    {
            cout << a << " " << b << " " << c << endl;
    }
    
    int main (void)
    {
        testclass objekt1(10,15);
        testclass objekt2(10,15);
        testclass temp = objekt1 + objekt2;
        temp.print ();
    }
    

    Da du keine Klassendeklaration gepostet hast, habe ich mal eine angenommen.
    Bei mir kommt 20 30 4 raus!

    Moritz



  • 1. datei classes.h

    void printPrimi(double d);
    class testclass
    {
    private:
    int a;
    double b;
    float c;
    
    public:
    testclass();
    testclass(const testclass &test);
    testclass(int A, double B, float C);
    testclass(testclass &);
    ~testclass();
    testclass operator+(testclass);
    void print();
    };
    

    datei classes1.cpp

    #include <iostream.h>
    #include "classes.h"
    
    static int iCounter;
    
    testclass::testclass(const testclass &test){iCounter++;}
    testclass::testclass(testclass &test){iCounter++;}
    testclass::testclass(){iCounter++;}
    testclass::testclass(int A, double B=5.0, float C=2.0):a(A),b(B),c(C){iCounter++;}
    testclass::~testclass()
    {
    cout<<"Ich bin ein Objekt und gehe nun den Weg des Irdischen"<<endl;
    }
    testclass testclass::operator+(testclass obj)
    {
    testclass help;
    help.a = a + obj.a;
    help.b = b + obj.b;
    help.c = c + obj.c;
    return help;
    }
    void testclass::print()
    {
    cout<<a<<" "<<b<<" "<<c<<endl;
    }
    
    void printPrimi(double d)
    {
    cout<<"Anzahl der Konstruktoraufrufe: "<<d<<endl;
    }
    void update(double &d){d++;}
    
    int main(int argc, char *argv[])
    {
    double j = 10;
    const double &r=j;
    char a;
    testclass objekt1(10,15);
    testclass objekt2(10,15);
    
    testclass &ref = objekt1;
    objekt1.print();    //Ausgabe 10,15,2
    objekt2.print();    //Ausgabe 10,15,2
    testclass temp(objekt1.operator+(objekt2));
    
    testclass objekt3(ref);
    objekt3.print();  //hier bekomme ich schon eine Ausgabe mit Speichermüll
    objekt1.~testclass();
    printPrimi(iCounter);
    cout<<j<<endl;
    temp.print(); //Zerstörung des obj1 müsste egal sein hab ja Werte in temp
    cin>>a;// nur damit Konsole anhält
    
      return 0;
    }
    


  • Lazarus schrieb:

    1. datei classes.h

    ...
    
    testclass::testclass(const testclass &test){iCounter++;}
    testclass::testclass(testclass &test){iCounter++;}
    // warum zweimal? die const-Version reicht!
    // außerdem kopiert der Kopierkonstruktor nichts
    
    ...
    
    int main(int argc, char *argv[])
    {
    
    ...
    
    testclass objekt1(10,15);
    
    ...
    
    testclass &ref = objekt1;
    
    ...
    
    testclass objekt3(ref);
    objekt3.print();  //hier bekomme ich schon eine Ausgabe mit Speichermüll
    
      return 0;
    }
    

    Klar, daß du da nur Speichermüll bekommst: dein Kopierkonstruktor kopiert ja nichts, sondern inkrementiert nur den Instanzenzähler (der übrigens besser als statische Klassenvariable deklariert werden sollte)! Mach es so:

    [cpp]
    testclass::testclass(const testclass &test)
    : a (test.a), b (test.b), c (test.c)
    {
    iCounter++;
    }
    [/cpp]

    Schau dir mal mein Posting oben an, da sind noch ein paar Optimierungsvorschläge drin...

    Moritz



  • Jo danke dir erst mal.

    cu


Anmelden zum Antworten