#include #include class Integer{ public: Integer(int value): value_(value) { copy_cnt = new int(0); std::cout << "Creating original:"<< value << std::endl; }; Integer(const Integer& src): value_(src.value_), copy_cnt(src.copy_cnt) { (*copy_cnt) += 1; std::cout << "Creating copy:"<< *copy_cnt << " of:" << value_ << std::endl; }; virtual ~Integer() { if(*copy_cnt == 0){ std::cout << "Deleting last: "<< value_ << std::endl; delete copy_cnt; } else{ (*copy_cnt) -= 1; std::cout << "Deleting: "<< value_ << "\t\t "<< *copy_cnt << " copies are left" << std::endl; } }; const Integer& operator=(const Integer& src){ if(&src != this) { this->value_ = src.value_; if(*copy_cnt == 0){ delete copy_cnt; }else{ (*copy_cnt) -= 1; } copy_cnt = src.copy_cnt; (*copy_cnt) += 1; } return *this; }; bool operator==(const Integer& src) const{ return this->value_ == src.value_; }; bool operator!=(const Integer& src) const{ return this->value_ == src.value_; }; operator int() const{ return this->value_; }; void setValue(int value){ this->value_ = value; if(*copy_cnt > 0){ *copy_cnt -= 1; copy_cnt = new int; *copy_cnt = 0; } }; private: int value_; int *copy_cnt; }; #include "handle.h" int main() { using namespace std; list > lista1; cout << "---- Skapar orginal ----" << endl; Integer a(1); Integer b(2); Integer c(3); cout << "---- Lägger dem i lista1 ----- " << endl; cout << "a\n"; lista1.push_front(a); cout << "b\n"; lista1.push_front(b); cout << "c\n"; lista1.push_front(c); list > *lista2; cout << "---- Skapa en ny lista med samma dataobjekt (lista2) ----" << endl; lista2 = new list >(lista1); cout << "---- Pop_back lista2----" << endl; lista2->pop_back(); cout << "---- Push_back lista2 med b ----" << endl; lista2->push_back(b); cout << "---- Sök effter 2 ----" << endl; Integer toFind = 2; list >::iterator it; it = find(lista2->begin(),lista2->end(),toFind); if(it != lista2->end()) cout << "Hittat" << endl; cout << "---- Sök effter 5 ----" << endl; toFind = 5; list >::iterator it0; it0 = find(lista2->begin(),lista2->end(),toFind); if(it0 == lista2->end()) cout << "Inte Hittat" << endl; cout << "--- Ändra på första elementet genom tilldelning ---" << endl; *lista2->front() = 4; cout << "---- Lista 1 ----" << endl; list >::iterator it2; it2 = lista1.begin(); while(it2 != lista1.end()) { cout << **it2 << " "; it2++; } cout << endl; cout << "---- lista 2 ---- " << endl; list >::iterator it3; it3 = lista2->begin(); while(it3 != lista2->end()) { cout << **it3 << " "; it3++; } cout << endl; cout << "---- Ta bort lista2 ----" << endl; delete lista2; cout << "---- Slut på program ----" << endl; return 0; }