Solved

C++ array of Classes like array of Integers?

Posted on 2013-12-10
2
259 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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

744 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

15 Experts available now in Live!

Get 1:1 Help Now