Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically create an array of object pointers

I have a class called "poly". I want to dynamically create an array of pointers to poly objects. The variable "totalPolynomials" holds the number of poly objects.

Here is my code to declare the array:

poly **polyPtr;                         
polyPtr = new poly *[totalPolynomials];

I successfully create poly objects, but don't know how to store their pointers in the array one after another...

like image 699
user3554066 Avatar asked May 08 '26 06:05

user3554066


1 Answers

If you are creating them and then want to store them, you can do something like

poly ** polyPtr;
polyPtr = new poly* [totalPolynomials];

for(int i = 0; i<totalPolynomials; ++i)
{
    polyPtr[i] = new poly(arguments);
}
like image 190
riklund Avatar answered May 10 '26 19:05

riklund



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!