HI. I am beginner in classes. how to work with dynamic memory in classes. I am not able to allocate dynamic array simply in public or private field. so instead allocated the dynamic array in a constructor. but how to access it later either int main() or some other methods of classes like get function to get number stored in that index. eg:
class vectorofint
{
public:
vectorofint();
int set(int index);
private:
};
vectorofint::vectorofint()
{
int *array=newint[32];
}
int vectorofint::set(int index)
{
array[index]=8;///says array is out of scope. how to access the array???
}
class vectorofint
{
public:
vectorofint();
int get(int index);
void set(int index);
private:
int *array;
};
void vectorofint::set(int index)
{
array[index]=8;
}
vectorofint::vectorofint()
{
int *array=newint[32];
}
int main()
{
class a;
a.set(8);///gives error at this line
}
is it necessary to delete it? just a little bit of memory dynamically allocated. don't see why it should run out of memory.
also even if i delete it using a destructor, still doesn't solve the problem. destructor used: