/* ************************************************************************** zadanie domowe nr: 3 plik: elem.cpp ************************************************************************** Poprawiał : CEZARY PRZYWUSKI ID212 Zmiany w pliku w liniach 72 - 99 */ #include #include #include "elem.h" elem::elem () { pnext = 0; cout << "konstruktor klasy elem nr." << id << endl; } elem::~elem () { if (pnext) delete pnext; cout << "destruktor klasy elem nr."<< id << endl; } elem * elem::insert_after (elem * ael) { if (!pnext) return (pnext = ael); if (!ael) return 0; ael->set_next (pnext); return pnext = ael; } elem * elem::set_next (elem * ael) { if (!ael) return 0; return pnext = ael; } elem_str::elem_str (char *val) { cout << "konstruktor klasy elem_str" << endl; if (val) { my_val = new char[strlen (val) + 1]; strcpy (my_val, val); } else my_val = 0; } elem_str::~elem_str () { if (my_val) cout << "destruktor elem_str, przechowywana wartosc: " << my_val << endl; else cout << "destruktor elem_str - brak wartoœci" << endl; if (my_val) delete[]my_val; } /* Bardzo niepoprawna implementacja operatora [] */ elem_str &elem_str::operator[](int indeks) { if(indeks==id) { return *this;} // w wypadku gdy szukany indeks jest w tym obiekcie zwracamy siebie else { if( !get_next() ) // sprawdzamy czy istnieje następny obiekt { elem_str *px=(elem_str *)get_next(); return px[indeks]; // jeśli tak to wywołujemy to rekurencyjnie :( , ale to jest fatalny pomysl , } } } /* Przeciążenie operatora przyrówania */ const elem_str &elem_str::operator = (const elem_str &prawy) { if(prawy.my_val) // czy nie wpisujemy nulla { delete [] my_val; // zwalniamy pamięć po starym napisie my_val = new char[strlen(prawy.my_val)]; // tworzymy nowy łańcuch o długości wpisywanego napisu strcpy(my_val,prawy.my_val); // lub bardziej inteligentna /* for(int i=0;i