Jakie moga byc przyczyny?
Czy T
to parametr szablonu czy nazwa funkcji(klasy, struktury)?
Zaznaczasz, że piszesz w C++ i używasz funkcji z C do alokowania pamięci.
Skorzystaj z std::unique_ptr<>
lub innego smart_pointera.
template <typename T>
class Foo
{
T val;
public:
Foo(){}
T getVal(){ return val; }
void setVal(int num){ val = num; }
};
int main()
{
const int SIZE = 2;
std::unique_ptr<Foo<int>[]> Queue(new Foo<int>[SIZE]);
Queue[0].setVal(5);
Queue[1].setVal(6);
for (int i = 0; i < SIZE; i++)
{
std::cout << Queue[i].getVal() << ' ';
}
}
Opcja dla malloc()
Foo<int>* values;
values = (Foo<int>*)malloc(SIZE * sizeof(Foo<int>*));
values->setVal(10);
(values + 1)->setVal(20);
for (int i = 0; i < SIZE; i++)
{
printf("%i ", (values + i)->getVal());
}