Solved

Dynamic Arrays inside Classes

Posted on 2007-04-09
6
269 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

773 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