Witam,
Po dość zawiłym tytule wyjaśnię o co chodzi. Zadanko mam takie: Klasy B,C,D dziedziczą po A. A list dziedziczy po list<A*>
i może przechowywać A, B, C, D. do Alist napisz konstruktor, destruktor, konstruktor kopiujący, operator =, operator +. Problem w moim kodzie jest w operatorze + i coś nie wiem chbya jeszcze o dziedziczeniu bo nie mogę go zdemaskować. Oto kod:
#include <cstdlib>
#include <iostream>
#include <list>
using namespace std;
class A
{
public:
virtual ~A() {}
};
class B : public A
{
public:
B() {}
~B() {}
void wypisz()
{
cout << "B" << endl;
}
};
class C : public A
{
public:
C() {}
~C() {}
void wypisz()
{
cout << "C" << endl;
}
};
class D : public A
{
public:
D() {}
~D() {}
void wypisz()
{
cout << "D" << endl;
}
};
class Alist : public list<A*>
{
Alist() {}
~Alist()
{
this->clear();
}
Alist& operator=(const Alist& a)
{
if (*this != a)
{
this->~Alist();
*this = a;
}
return *this;
}
Alist(const Alist& a)
{
*this = a;
}
Alist& operator+(const Alist& a)
{
Alist::iterator i;
for (i = a.begin(); i < a.end(); ++i)
this->push_back(*i);
return *this;
}
};
int main()
{
Alist a;
a.push_back(new B);
a.push_back(new C);
list<A*>::iterator i;
for (i = a.begin(); i < a.end(); ++i)
(*i).wypisz();
system("PAUSE");
return 0;
}
Z góry dziękuję za wszelkie sugestie :)