Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize an array in C++ objects

After reading How to initialize an array in C, in particular:

Don't overlook the obvious solution, though:

int myArray[10] = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };

I tried something like this:

#include <iostream>

class Something {
private:

int myArray[10];

public:

Something() {
    myArray[10] = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };
}

int ShowThingy(int what) {
    return myArray[what];
}

~Something() {}
};

int main () {
   Something Thing;
    std::cerr << Thing.ShowThingy(3);
}

And I get:

..\src\Something.cpp: In constructor 'Something::Something()':
..\src\Something.cpp:10:48: error: cannot convert '<brace-enclosed initializer list>' to 'int' in assignment

The obvious in this case is not so obvious. I really would like the initiation of my array to be more dynamic as well.

I tired:

private:
    int * myArray;

public:
    Something() {
            myArray = new int [10];
            myArray = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };
}

This looked funky to me to, and so to the compiler:

..\src\Something.cpp: In constructor 'Something::Something()':
..\src\Something.cpp:11:44: error: cannot convert '<brace-enclosed initializer list>' to 'int*' in assignment

This also did not work:

private:
int myArray[10] = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };

with:

 ..\src\Something.cpp:6:20: error: a brace-enclosed initializer is not allowed here before '{' token
 ..\src\Something.cpp:6:51: sorry, unimplemented: non-static data member initializers
 ..\src\Something.cpp:6:51: error: 'constexpr' needed for in-class initialization of static data member 'myArray' of non-integral type

I have been doing really good and learning what does not work, but not so good learning what does work.

So, how do I used initialization lists {value, value, value} for an array inside a class?

I have been trying to figure out how to do this for some time now and am very stuck, I have a number of these kinds of lists I need to make for my app.

like image 604
Quade2002 Avatar asked May 22 '12 01:05

Quade2002


People also ask

Can array objects be initialized?

One way to initialize the array of objects is by using the constructors. When you create actual objects, you can assign initial values to each of the objects by passing values to the constructor. You can also have a separate member method in a class that will assign data to the objects.

How do you initialize an array?

There are two ways to specify initializers for arrays: With C89-style initializers, array elements must be initialized in subscript order. Using designated initializers, which allow you to specify the values of the subscript elements to be initialized, array elements can be initialized in any order.

How do you declare an array of 10 objects of a class?

Syntax: Class_Name obj[ ]= new Class_Name[Array_Length]; For example, if you have a class Student, and we want to declare and instantiate an array of Student objects with two objects/object references then it will be written as: Student[ ] studentObjects = new Student[2];


1 Answers

You need to initialize the array in the constructor initialization list

#include <iostream>

class Something {
private:

int myArray[10];

public:

Something()
: myArray { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 }
{
}

int ShowThingy(int what) {
    return myArray[what];
}

~Something() {}
};

int main () {
   Something Thing;
    std::cerr << Thing.ShowThingy(3);
}

..\src\Something.cpp:6:51: sorry, unimplemented: non-static data member initializers

C++11 also adds supports for inline initialization of non-static member variables, but as the above error message states, your compiler has not implemented this yet.

like image 75
Praetorian Avatar answered Nov 15 '22 14:11

Praetorian