Copy constructor & rule of three
Në shqip: Konstruktori i kopjimit
ShpjegimiSQ
Konstruktori i kopjimit vendos çfarë ndodh kur krijon një objekt si kopje të një tjetri. Nëse klasa jote menaxhon memorie me pointer-ë, kopja e paracaktuar kopjon vetëm adresën — dhe dy objekte e fshijnë të njëjtën memorie. „Rregulli i treve“ thotë: po shkrove destruktor, shkruaj edhe kopjimin dhe caktimin.
EnglishEN
The copy constructor decides what happens when you create an object as a copy of another. If your class manages memory with pointers, the default copy only copies the address — and two objects end up deleting the same memory. The “rule of three” says: if you write a destructor, also write copying and assignment.
Si ta mendosh
Si ta fotokopjosh një çelës kasafortë: po kopjove vetëm etiketën „kasaforta 3“, të dy kanë të njëjtën kasafortë — po e boshatisi njëri, e gjen bosh edhe tjetri.
Lexoje në anglisht
Like copying a safe-deposit key: copy just the “box 3” label and you both share the same box — if one empties it, the other finds it empty too.
Shembull kodicpp
class Liste {
int* te_dhenat;
public: Liste(const Liste& tjeter); // kopjimi
~Liste(); // destruktori
};