C++ array of Classes like array of Integers?

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]?
deleydAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Carl BohmanConnect With a Mentor Commented:
You can have an array of any type (including classes):
int a1[10];
MyClass a2[10];

Open in new window

0
 
Carl BohmanConnect With a Mentor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.