-1

When declaring an array of objects, are all the objects constructed at initialization, or do they have to be constructed after initialization? Here is an example of what I am trying to explain:

Lets say I have this class:

class Object{
public:
    int x = 4;
};

And this array:

Object objects[8];

If I was to access any of the variables within the objects, would I have to construct the objects first, or was that done in the array? So if I did this:

cout << objects[4].x;

Would it print out 4?

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
  • If they had to be constructed after initialization, how would you do it? There's no syntax for initializing a variable. – Barmar Sep 13 '16 at 00:43
  • This `int x = 4;` is invalid for C++. You have to explicitly initialize the variable in a constructor. Did you even try running your code? Also, the answer is: yes, it will print `4`. – Jezor Sep 13 '16 at 00:43
  • 5
    @Jezor Initializing a member like that [is perfectly valid since C++11](https://en.wikipedia.org/wiki/C%2B%2B11#Object_construction_improvement). – Captain Obvlious Sep 13 '16 at 00:44
  • @Jezor What do you mean? I am just declaring a global variable. – William Thomas Sep 13 '16 at 00:46
  • @CaptainObvlious "since C++11", which is not enabled by default in most compilers, but yeah, you're right. – Jezor Sep 13 '16 at 00:47
  • @WilliamThomas it's not global, it's a part of `Object` class. To make it global, you'd have to use the `static` keyword. – Jezor Sep 13 '16 at 00:48

2 Answers2

2

In C++11 your code is perfectly valid, it performs in-class initialization, and indeed, cout << objects[4].x; will print out 4. In previous C++ versions (C++98/03), the code is invalid, and you'd need a default constructor to initialize the variable x, like

class Object{
public:
    int x;
    Object(int x = 4): x(x){}
}
vsoftco
  • 55,410
  • 12
  • 139
  • 252
  • It was pointed out to me many times that answering a question that is low-quality is a bad practice. – Jezor Sep 13 '16 at 00:50
  • Okay, so when I declare an array of integers, I would do `int arr[8] = {};` to Have all the cells equal to zero, but when I declare an array of objects, I don't have to do anything that would in essence "construct" the objects? – William Thomas Sep 13 '16 at 00:50
  • @Jezor I prefer to answer a question rather than writing an answer in a comment. I believe comments are for *comments* and not writing answers. And I don't find this question as a low-quality. It is reasonable, the OP posted some code, and (s)he may be a beginner. – vsoftco Sep 13 '16 at 00:50
  • @vsoftco as it was explained to me, if you answer a low-quality question, it's more difficult to close and delete it. – Jezor Sep 13 '16 at 00:52
  • 1
    @Jezor No, any user that has a C++ gold badge can close this question at once. Delete-ing it requires 3 votes, which is not a lot, so if other people believe that this question is indeed trash, they can close/delete it. Remember that all of us were beginners some time. At least this question is well formulated, has some code posted, and asks a very specific thing. It may be a dupe indeed, but I won't consider it as a low-quality. – vsoftco Sep 13 '16 at 00:54
  • @vsoftco well, OP didn't even bother to try and see if his / hers code works as expected, that's why I consider it low quality. Anyways, if you don't mind, I'm going to refer to your answer from now on when somebody says I shouldn't post my answer. – Jezor Sep 13 '16 at 01:04
  • @WilliamThomas Regarding your question in the comment: when you have an array of objects, the compiler performs default-initialization (if there is a default constructor) of all objects in the array, and in C++11 in addition it performs in-class member-initialization. – vsoftco Sep 13 '16 at 01:05
  • 2
    @Jezor Fair enough. I **strongly** believe that answers have to be written as answers, and comments reserved only for comments. In this way, one can judge the quality of the answer. A comment can only be upvoted, but not downvoted (except other comments which most of the time end up in an endless useless fight). And you should just ignore those that tell you that you shouldn't post an answer. We live in a free world after all :) – vsoftco Sep 13 '16 at 01:06
1
  1. are all the objects constructed at initialization

Yes, all of elements of array will be default initialized.

if T is an array type, every element of the array is default-initialized;

And

if T is a non-POD (until C++11) class type, the constructors are considered and subjected to overload resolution against the empty argument list. The constructor selected (which is one of the default constructors) is called to provide the initial value for the new object;

And

  1. Would it print out 4?

Yes. For class Object, the implicitly-declared default constructor will be invoked here. And the member x is not initialized by the member initializer list (in default constructor), the default member initializer is applied, then x will be initialized with value 4.

songyuanyao
  • 169,198
  • 16
  • 310
  • 405