copyconstructor baut misst
-
hi,.. leuts,..
also im ersten fall funktioniert der copyconstructer wie er sollte,. im zwoten wieder nicht,.. meine frage wieso????
(...weitere erläuterung weiter unten...)hier einmal der wesentliche source für die ausgabe:
//---class whole_number_Type whole_number_type::whole_number_type(const whole_number_type &b) { cout<<endl<<"copy constructor:"<<endl; count=b.count; delete pBinary_array; pBinary_array=new bool[count];//cout<<endl; for(long int i=0;i<count;i++){pBinary_array[i]=b.pBinary_array[i];} for(long int i=0;i<count;i++){ cout<<b.pBinary_array[i];} sign_of_binary_array=b.sign_of_binary_array; cout<<endl; }; whole_number_type &whole_number_type::operator+(const whole_number_type &obj) { whole_number_type a(*this); long int counting=0; bool *aArray; bool *bArray; bool *cArray; bool *car_ray; bool carry=0; bool old_carry=0; //first we have to check which #counts are higher if(count >= obj.count) {counting=a.count+1;} else {counting=obj.count+1; }; //check signes aArray=new bool[counting]; bArray=new bool[counting]; cArray=new bool[counting]; car_ray=new bool[counting]; for(long int i=0;i<counting;i++) {aArray[i]=false; bArray[i]=false; cArray[i]=false;car_ray[i]=false;}; for(long int i=0;i<obj.count;i++) {aArray[i]=a.pBinary_array[i];}; for(long int i=0;i<obj.count;i++) {bArray[i]=obj.pBinary_array[i];}; //cout<<endl; //for(long int i=0;i<counting-1;i++){cout<<aArray[i];};cout<<endl; // for(long int i=0;i<counting-1;i++){cout<<bArray[i];}; if((a.sign_of_binary_array)&&(obj.sign_of_binary_array))//k we can add { bool temp=0; //cout<<endl; for(long int i=0;i<counting-1;i++) { temp=(aArray[i]^bArray[i]); car_ray[i+1]=((aArray[i]&&bArray[i])||(temp&&car_ray[i])); cArray[i]=car_ray[i]^temp; // cout<<aArray[i]<<bArray[i]<<car_ray[i]<<" "<<cArray[i]<<endl; } a.sign_of_binary_array=obj.sign_of_binary_array; } else{ } delete a.pBinary_array; //cout<<counting-1; a.count=counting; a.pBinary_array=new bool[counting]; //cout<<endl; a.pBinary_array[counting-1]=car_ray[counting-1]; for(long int i=0;i<counting-1;i++) {a.pBinary_array[i]=cArray[i];cout<<a.pBinary_array[i];} cout<<a.pBinary_array[counting-1]<<endl; return a; }; //-----main whole_number_type k(8); whole_number_type g(8); whole_number_type h(8); k=one;cout<<endl; h=two;cout<<endl; g=k+h;cout<<endl; k.print(); h.print(); g.print(); whole_number_type i(8); whole_number_type j(8); whole_number_type l(8); i=three;cout<<endl; j=four;cout<<endl; l=i+j;cout<<endl; i.print(); j.print(); l.print();ok und hier die ausgabe:
copy constructor: 01011001 000010110 <- 1.a copy constructor: 000010110 <- 1.b 01011001 01101100 000010110 <- 1.c copy constructor: 01110110 101000010 <- 2.a copy constructor: 000000010 <- 2.b ?????? 01110110 11101000 000000010 <- 2.c Drücken Sie eine beliebige Taste . . .ok,. fall x.a ist das ergebniss der addition,
der fall x.b der aufruf des copyconstructors vor '='
und x.c der aufruf der methode print(),...
kann mir jemand helfen?? ich habe keine ahnung woran das liegt,..seid gegrüßt
-
Warum ruft dein Copykonstruktor delete auf? Es gibt keinen "alten" Inhalt, der gelöscht werden müsste.
-
weil sonst dass passiert:
copy constructor: 160261016026100und ich bin hier praktisch
gruuuß
-
zeusosc schrieb:
weil sonst dass passiert:
copy constructor: 160261016026100Mag ja sein, ist trotzdem falsch.
Dein operator+ gibt übrigens eine Referenz auf eine lokale Variable zurück.
Warum plagst du dich eigentlich mit Arrays ab, und benutzt nicht std::vector?
-
weil geplant ist mehrere tausend klassen aufzurufen,..
und ::vector braucht dann ne menge speicherplatz,..
da habe ich halt gehofft das ein bool'sches array net so viel braucht,..gruuß
-
zeusosc schrieb:
weil geplant ist mehrere tausend klassen aufzurufen,..
und ::vector braucht dann ne menge speicherplatz,..Ein vector<bool> dürfte um ein vielfaches effizienter sein (zumindest was den Speicherverbrauch angeht) als dein Array-Gebastel. Mit dem Zusatznutzen, dass es funktioniert, und du weder Copykonstruktor noch Zuweisungsoperator oder Destruktor selbst zu schreiben brauchst.
zeusosc schrieb:
da habe ich halt gehofft das ein bool'sches array net so viel braucht,..
Du weißt aber, dass ein vector innen drin normalerweise auch nur ein Array ist, oder?
-
so weit ich weiß is std::vector ein container,...
gruuuß
-
ja, std::vector ist ein container, aber der verwendet in der regel intern einfach ein array, das mit new dynamisch erzeugt wird.
und ein array von bool[] verbraucht sogar *mit sicherheit* mehr speicher als ein vector<bool>, denn sizeof(bool) ist niemals weniger als ein byte und vector<bool> packt idR bools intern tatsächlich in einzelne bits.vector<bool> ist übrigens kein container.
-
ok,. danke für die info,..
ich verbleibe erstmal bei einem bool array[] und würde gerne wissen wo im copyconstructor der fehler liegt, bzw. das gerade beim ersten alles richtig läuft und beim zweiten aufruf ein paar bits verschluckt werden,....
gruuuß
-
zeusosc schrieb:
ich verbleibe erstmal bei einem bool array[] und würde gerne wissen wo im copyconstructor der fehler liegt, bzw. das gerade beim ersten alles richtig läuft und beim zweiten aufruf ein paar bits verschluckt werden,....
Wie ich schon sagte:
Das delete im Copykonstruktor ist falsch.
Der operator+ gibt eine Referenz auf eine lokale Variable zurück, die nach dem Aufruf nicht mehr existiert.Das erzeugt undefiniertes Verhalten. Damit ist es müßig, sich darüber Gedanken zu machen, was das Programm tut, weil es alles tun könnte.
-
ohhhhh man (eigene doooofheit),. super

ja,. funzt,.. danke leutz
-
Wie MFK schon sagte, es gibt nicht altes zu löschen.
Falls doch müsstest Du dann aber schon den delete[] nehmen.
-
Int_t *my_ints = new Int_t[10];
...
delete []my_ints;
This tells the delete operator to go and find the array size, and delete all the objects.
k,.. danke für den tipp
