witam,
mam taką klasę
template <class T> class Test : public ITest<T>
{
public:
Test(): atrybuty(NULL),size(0){}
Test(const long int &_size)
{
try
{
atrybuty=new T[_size];
this->SetSize(_size);
}
catch(bad_alloc *ex)
{
cout<<ex->what()<<endl;
}
}
~Test()
{
if(atrybuty!=NULL)
delete []atrybuty;
}
...jakiś kod...
T& operator [ ](const int &i)
{
try
{
return atrybuty[i];
}
catch(bad_exception *ex)
{
cout<<ex->what()<<endl;
}
}
...jakiś kod...
private:
void SetSize(const long int &_size)
{size=_size;}
T *atrybuty;
int size;
};
i np. napiszę taki kod:
Test<string> test(2);
test[0]="ala";
test[1]="ma";
test[2]="kota";
cout<<test[0]<<" "<<test[1]<<" "<<test[2]<<endl;
to wszystko działa, ale dla poniższego już nie:
Test<string> *test=new Test<string>(3);
test[0]="ala";
test[1]="ma";
test[2]="kota";
cout<<test[0]<<" "<<test[1]<<" "<<test[2]<<endl;
Nie jestem orłem ze wskaźników i być może popełniam jakiś banalny błąd.
z góry dzięki za pomoc