Link to home
Start Free TrialLog in
Avatar of Serph
SerphFlag for United Arab Emirates

asked on

Dynamic Arrays inside Classes

Hello,

I'm trying to write a class that contains data and a dynamic array of the same class type.

Is that possible?And how can I write that?
ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of KurtVon
KurtVon

I'll even go one further and say this works fine:

#include <vector>
   
class DynArrayTest{
public:
    DynArrayTest();
    ~DynArrayTest();
   
    std::vector<DynArrayTest> testArray;
};
That's basically the same, KurtVon. As long as you don't include an instance of the class in the class itself, it's fine. So, this will NOT work :

class Test {
  Test dyn_arr;
};

for obvious reasons (you'd have an infinite inclusion, and an infinite size of the Test class).

The example KurtVon gave also works, because the elements of the vector are not stored in the class body, but rather a "reference" to the elements.
May I ask why you gave a B grade ? Is something still unclear ? If so, you can always ask for clarification ... You don't need to accept a reply that doesn't fully answer your question !
Avatar of Serph

ASKER

I thought I gave it an A?! Must have pressed enter when I selected A?
How can I correct this? *Sorry!*
There's no problem. I just wanted to make sure that you didn't select the reply without understanding it.

Requests for modifications can be done by posting a 0-point question in the Community support area. But as I said, there's no need, as long as I know that the reply was clear to you :)