Hi there i have some questions about pointers and arrays in c++:
when i want to pass an array of pointers in a function i must write:
foo(test *ptest[])
right?
when i want to delete a array of pointers, does
delete ptest;
all for me?
when i pass a pointer to another class and the first class make a delete, the pointer is deletete in all classes right?
must i always create a array of a constant size?
First of all, forget about arrays and pointers and tell us what you really want to achieve. You are probably better off using
std::vector<T>and pass that around by (const) reference, saving you all the headaches of manual resource management and crazy array-to-pointer-decay rules inherited from C.Now to answer your questions:
Yes, but remember that array parameters are always rewritten by the compiler to pointers. Your code is equivalent to:
This means that all information about the size of the array has been lost. Moving on:
No. If you acquire via
new[], you must release viadelete[], otherwise you get undefined behavior. Note that deleting an array of pointers only gets rid of the array itself, not any objects pointed to by the pointers stored inside the array.Again: Forget about arrays and pointers and use the facilities provided by the C++ standard library.