Solved

Dynamic Arrays inside Classes

Posted on 2007-04-09
6
277 Views
Last Modified: 2013-12-14
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?
0
Comment
Question by:Serph
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 125 total points
ID: 18877495
class Test {
  Test *dyn_arr;
};
0
 
LVL 11

Expert Comment

by:KurtVon
ID: 18878773
I'll even go one further and say this works fine:

#include <vector>
   
class DynArrayTest{
public:
    DynArrayTest();
    ~DynArrayTest();
   
    std::vector<DynArrayTest> testArray;
};
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18878842
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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 53

Expert Comment

by:Infinity08
ID: 18883370
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 !
0
 
LVL 1

Author Comment

by:Serph
ID: 18883425
I thought I gave it an A?! Must have pressed enter when I selected A?
How can I correct this? *Sorry!*
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18883508
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 :)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question