Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

895 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

17 Experts available now in Live!

Get 1:1 Help Now