Solved

C++ array of Classes like array of Integers?

Posted on 2013-12-10
2
265 Views
Last Modified: 2013-12-10
In C++ can I have an array of Classes which I can randomly access with an index {0,1,2,3,...} the way I can have an array of Integers accessed as a[k]?
0
Comment
Question by:deleyd
  • 2
2 Comments
 
LVL 13

Accepted Solution

by:
Carl Bohman earned 200 total points
ID: 39708456
You can have an array of any type (including classes):
int a1[10];
MyClass a2[10];

Open in new window

0
 
LVL 13

Assisted Solution

by:Carl Bohman
Carl Bohman earned 200 total points
ID: 39708465
You may also want to have a look at the vector class (part of the Standard Template Library or STL) for an object-oriented array.  There are plenty of other array-like structures in the STL as well.

One more thing to point out is that an array of items (classes or not) contains only copies of what is placed in them.  If you want to reference the original objects or variables, you'll need to have an array of pointers.
MyClass *a3[10];

Open in new window

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

Title # Comments Views Activity
What does std::atomic give me? 7 113
C++ BOOL WINAPI ReadFile fails on windows 10 when reading from USB cable 9 371
mixing C++ & C# in Vis Studio 2013 7 139
c++ syntax question 9 43
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

943 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