I have a user-defined class Person and I want to create a variable-sized array (size taken at runtime) of Person pointers.
The following code works as expected.
Person **arr = new Person* [size];
However, I didn't understand exactly whom the * operator is associated with. I tried putting paranthesis like:
new ((Person*)[size])new (Person (*)[size])
Only the second option works.
This seems counterintuitive as in a regular static declaration,
Person* arr[10] would mean an array of Person* type (Array of Person pointers) (associativity with Person).
While Person (*arr)[10] would mean a pointer to an array of Person type (associativity with arr).
Edit: As pointed out by @Jan Schultke I realised that new Person* [size] and new (Person (*)[size]) don't have the same effect. One returns Person** and the other returns Person (**)[size].
Also, the second does not allow size to be a variable.
Again, I know I can use vector for dynamic arrays. I am learning OOP methodology lately, focussing on the concepts of programming language C++, and my aim is to get on grips with what goes on under the hood, rather than simply using a library.
new(outside of datastructure internals) you're not using C++ as it is intended to be used. Even though many "teachers" still teach you to use "new".