Solved

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

Posted on 2015-01-23
4
74 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
jkr, thank you for your input.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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…

762 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

12 Experts available now in Live!

Get 1:1 Help Now