5

I have a basic C++ class .The header looks like this:

#pragma once
class DataContainer
{
public:
    DataContainer(void);
   ~DataContainer(void);
    int* getAgeGroup(void);
    int _ageGroupArray[5];
private:

     int _ageIndex;

};

Now inside the cpp file of the class I want to intialize the _ageGroupArray[5] with default values inside the class contructor like this:

#include "DataContainer.h"


DataContainer::DataContainer(void)
{

_ageGroupArray={20,32,56,43,72};

_ageIndex=10;
}

int* DataContainer::getAgeGroup(void){
return _ageGroupArray;
}
DataContainer::~DataContainer(void)
{
}

Doing it I am getting "Expression must be a modifiable lvalue" on _ageGroupArray line.So is it entirely impossible to initialize an array object in the constructor? The only solution I found was to define the array outside scope identifiers .Any clarification on this will be greatly appreciated.

Michael IV
  • 11,016
  • 12
  • 92
  • 223

3 Answers3

7

In the current standard, as you have already noticed, you cannot initialize a member array in the constructor with the initializer list syntax. There are some workarounds, but none of them is really pretty:

// define as a (private) static const in the class
const int DataContainer::_age_array_size = 5;

DataContainer::DataContainer() : _ageIndex(10) {
   int tmp[_age_array_size] = {20,32,56,43,72};
   std::copy( tmp, tmp+_age_array_size, _ageGroupArray ); 
}

If the values in the array are always the same (for all object in the class) then you can create a single static copy of it:

class DataContainer {
   static const int _ageGroupArraySize = 5;
   static const int _ageGroupArray[ _ageGroupArraySize ];
// ...
};
// Inside the cpp file:
const int DataContainer::_ageGroupArray[_ageGroupArraySize] = {20,32,56,43,72};
David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
5

You can Initialize a array when you Create/Declare it, not after that.

You can do it this way in constructor :

_ageGroupArray[0]=20;
_ageGroupArray[1]=32;
_ageGroupArray[2]=56;
_ageGroupArray[3]=43;
_ageGroupArray[4]=72;

It is important to know that this is Assignment & not Initialization.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
1

try this:

int ageDefault[]={20,32,56,43,72};
memcpy(_ageGroupArray, ageDefault, sizeof(ageDefault));
zabulus
  • 2,373
  • 3
  • 15
  • 27
  • 1
    That is kind of dangerous, as it will only work for POD types, and fail silently (undefined behavior) for all other types. – David Rodríguez - dribeas Jul 11 '11 at 07:42
  • 1
    @zabulus thanks ,that way it works.But can you explain shortly to a noob like me why I can't initialize the -ageGroupArray directly upon class instantiation (I mean without using memcpy ) ? – Michael IV Jul 11 '11 at 07:50
  • 1
    @Michael The simple answer is: because there is no syntax which supports it. (I think this has been fixed in C++11.) I think that the reason there is no syntax is that C style arrays are considered second class citizen in C++, sort of; if you used `std::vector`, you could use a two iterator constructor to construct it from a static C style array. Also, with regards to @David: he's right about `memcpy`, but there is a standard function `std::copy` which can be used. – James Kanze Jul 11 '11 at 07:54