Solved

C++ array of Classes like array of Integers?

Posted on 2013-12-10
2
270 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

Independent Software Vendors: 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

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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

734 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