Solved

C++ array of Classes like array of Integers?

Posted on 2013-12-10
2
269 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
[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
  • 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

Industry Leaders: 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…
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

733 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