Solved

Dynamic Arrays inside Classes

Posted on 2007-04-09
6
267 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

932 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

10 Experts available now in Live!

Get 1:1 Help Now