Dynamic Arrays inside Classes

Posted on 2007-04-09
Last Modified: 2013-12-14

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?
Question by:Serph
  • 4
LVL 53

Accepted Solution

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

Expert Comment

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

#include <vector>
class DynArrayTest{
    std::vector<DynArrayTest> testArray;
LVL 53

Expert Comment

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.
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

LVL 53

Expert Comment

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 !

Author Comment

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

Expert Comment

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 :)

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now