How to initialize an array in C ++ objects

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

Do not lose sight of the obvious solution:

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 initialization of my array to be more dynamic.

I'm tired:

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

This looked funny to me, and therefore 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 }; 

from:

  ..\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 am doing really well and learning what is not working, but not so good studying what works.

So, how did I use the initialization lists {value, value, value} for the array inside the class?

I tried to figure out how to do this for some time, and I was very stuck, I have several such lists that I need to do for my application.

+6
source share
2 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, not implemented: non-static data initializers

C ++ 11 also adds support for the built-in initialization of non-static member variables, but as indicated above, the error message indicates that your compiler has not yet implemented this.

+18
source

If I am mistaken, the list of initializers is allowed only when the variable is initialized during the declaration - hence the name. You cannot assign a list of initializers to a variable, as you are trying to do in most of your examples.

In the last example, you are trying to add static initialization to a non-static member. If you want the array to be a static member of the class, you can try something like this:

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

If you want to add a class member, you can try creating a static const array and copy it to the member array in the constructor.

+1
source

Source: https://habr.com/ru/post/916316/


All Articles