Hi,
How do I declare an array of pointers, which are pointing to a class I've created myself?
So then I can do something like this:
pnt_array[4] = new MyClass(5);
Thank you for any help.
Jonny Wong
Printable View
Hi,
How do I declare an array of pointers, which are pointing to a class I've created myself?
So then I can do something like this:
pnt_array[4] = new MyClass(5);
Thank you for any help.
Jonny Wong
Code:ClassName **pnt_array = new ClassName*[4];
and here is one way you could populate your array of pointers to your class object:
Code:for(int i=0; i<4; i++)
pnt_array[i] = new ClassName;
dereference your array like this:
Code:
pnt_array[i]->function() ;
free the array like this:
Code:
for(int i=0; i<4; i++)
delete pnt_array[i];
delete [] pnt_array;
Hi,Quote:
How do I declare an array of pointers, which are pointing to a class I've created myself?
Here is how you declare an array of int's:
int nums[20];
More generally, to declare any array you first list the type of the elements you want to store in the array, then the array name, then the size of the array between brackets. You said you want an array that will store pointers to your class, so that means the type of the elements you want to store in the array will be: YourClass*.