Solved

2. Can we declare an array of objects? Array of classes?

Posted on 2015-01-23
4
95 Views
Last Modified: 2015-01-23
Can we declare an array of objects? Array of classes?
0
Comment
Question by:Nusrat Nuriyev
[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
  • 3
4 Comments
 
LVL 86

Assisted Solution

by:jkr
jkr earned 500 total points
ID: 40567407
Yes, you can. That works just like an array of any other data, e.g.

class MyClass {

  // ...
};

int main() {

  MyClass array_of_myclass[42]; // declares an instantiates array of 42  ojects of 'MyClass'
}

Open in new window


As a side note, if you declare an array of objects, each element's default constructor is called.
0
 
LVL 86

Expert Comment

by:jkr
ID: 40567410
BTW, for a more complex example, see the tutorial at https://www.hscripts.com/tutorials/cpp/array-of-objects.php
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 40567413
Also, since that seems common in your question: There are only arrays of instances of classes (aka objects), there's no such thing as an array of classes.
0
 

Author Comment

by:Nusrat Nuriyev
ID: 40567517
jkr, thank you for your input.
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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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…
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.
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.

617 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