Array Assignment in Constructor

Given an example class:

1
2
3
4
5
6
7
8
class foo
{
    public:
        foo();
        ~foo();
    private:
        int myArray[4];
};


I want to assign values to "myArray" upon construction of "foo" object. However, I would like to assign all of the values at once.

From what I understand, C++11 allows:
 
foo::foo() : myArray{1, 2, 3, 4} {}


...however, VS2012 is not allowing the above method.

I know that I can:
1. use a vector. I need to know about arrays.
2. initialize the array to zeros and assign individual values within the constructor. I'm curious about being able to assign all the values at once.

Thanks, in advance, for your help.
I believe that VS2012 is based on C++03, but it also has some C++11 functions mixed in. So some C++11 code might work, while others might not.

Hmm, this might work, but I don't have my compiler on me. So it probably has bugs. I remember doing something similar for this with a project I had, but it's been over 6 months since I last worked on that.

1
2
3
4
5
6
7
8
9
10
/* constructor */
Foo::Foo(int &array[4])
{
     /* iterate through your array */
     for (int i = 0; i < 4; i++)
     {
          /* set the value for your array */
          this->myArray[i] = array[i];
     }
}


1
2
3
4
5
6
7
8
/* create the array */
int *test = [0, 2, 3, 4];

/* create the foo object */
Foo foo(test);

/* don't forget to clean up the array */
delete test;


I'm not sure how to do this all in one line. (Other than separating it out with ; But that is kindof lame.) I've been using C++11 for sometime now and it is amazing.

I'll fix the code when I get home.

Good Luck,
MaxterTheTurtle

Edit:
Maybe?
 
Foo foo(new int[0, 2, 3, 4]);
Last edited on
@MaxterTheTurtle
A couple things:

Foo::Foo(int &array[4])
brings up an exception, did you mean
Foo::Foo(int &array) ?

int *test = [0,2,3,4];
also throws an exception.

The arrays in the class will not be defined by the user, they will just be contained within the object as private members. I was just hoping to be able to assign the values to the array members in one step, rather than individually.

I had considered making the arrays static. However, I'm not very versed on the memory implications behind static members and I have to conserve as much memory as possible.
The primary reason to use constructor initializer lists, other than it being good practice, is to avoid the overhead of default initialization follow by copy assignment when you can directly copy initialize.

However, in the case of a plain array like this, I don't think there is even any sort of default initialization at all, so assigning values in the body of the constructor would be just as efficient.

You will of course note different results between debug and release - debug builds generally assign special 'this is uninitialized' debug values, where as release mode will leave uninitialized data untouched.

...so assigning values in the body of the constructor would be just as efficient.


The array has to be a class member, and if the array is defined in the class definition, all values cannot be assigned in one statement.

If the array is a 2D array and/or very large, assignment to each array block can be very cumbersome. I was just hoping there was a way (work-around?) to do assignment to an array class member in a single statement. Perhaps this is not possible pre-C++11?
fatirishman53 wrote:
The array has to be a class member, and if the array is defined in the class definition, all values cannot be assigned in one statement.
You appear to have a misconception about how primitive arrays are initialized. The following two code snippets should generate identical machine code with a modern compiler:
1
2
3
4
5
int arr[] =
{
    1, 2, 3,
    4, 5, 6
};
1
2
3
4
int arr[6];

arr[0] = 1; arr[1] = 2; arr[2] = 3;
arr[3] = 4; arr[4] = 5; arr[5] = 6;
Do note that it is optimizations that cause both code snippets to generate identical machine code.

If however it is an array of objects, you would be correct - but why not use std::vector which is already optimized for this purpose and then further optimized since the compiler recognizes it?
Last edited on
Topic archived. No new replies allowed.