class BigInt { private: int * number; // simple pointer int ndig; // count digits .... // implementation file for class BigInt // BigInt::BigInt(char * str) { int len = strlen(str); number = new int[len+1];
Jetzt müssen Copy-Constructor, Zuweisungsoperator und Destructor definiert und (sorgfältig) implementiert werden
ansonsten Speicherüberschreiben, Memory-leaks, etc
class BigInt { private: int * number; // simple pointer int ndig; // count digits .... BigInt(const BigInt & x ); // copy const ~ BigInt(); // destructor BigInt & operator = (const BigInt & x); .... // implementation file for class BigInt // BigInt::BigInt(char * str) { int len = strlen(str); number = new int[len+1];// allocate memory ... BigInt::BigInt(const BigInt & b) { // copy constructor int len = b.ndig; number = new int[len];// allocate memory ... // Rest wie std constructor BigInt & BigInt::operator = (const BigInt & b); { // = constructor int len = b.ndig; number = new int[len];// allocate memory ... // Rest wie std constructor return (*this); } BigInt::~ BigInt() { // destructor delete[] number; number = NULL; ... }