# Initialising arrays in class constructor

**URL:** <https://community.particle.io/t/initialising-arrays-in-class-constructor/3295>\
**Category:** Getting Started\
**Created:** [March 7, 2014, 2:22pm UTC](https://community.particle.io/t/initialising-arrays-in-class-constructor/3295 "2014-03-07T14:22:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![timolol](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/timolol/32/2844_2.png) [@timolol](https://community.particle.io/u/timolol)\
**Post date:** [March 7, 2014, 2:22pm UTC](https://community.particle.io/t/initialising-arrays-in-class-constructor/3295/1 "2014-03-07T14:22:32Z")

</div>

Hi everyone,

A newbie question. I have created a class (simplified version), with the .h as below:

```
class LimitlessLED {
  public:
    LimitlessLED();
    void sendUDP(byte *cmd);
    void setBrightness(int level);
    void setColour(int col);
    void setBrightness(int gp, int level);
    void setColour(int gp, int clr);
    int control(String cmd);

  private:
      byte white_ALL_off[2];
};

```

With the constructor in .cpp:

```
LimitlessLED::LimitlessLED() {
  this->white_ALL_off = {0x39,0x00};
}

```

I get compilation error with `error: assigning to an array from an initializer list`

What is the problem with initialising default array variables in a constructor?

Thank you for your guidance! 😄

Regards,  
Tim

---

<div class="post-metadata">

**Author:** ![zachary](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/zachary/32/20717_2.png) [@zachary](https://community.particle.io/u/zachary)\
**Post date:** [March 11, 2014, 8:10pm UTC](https://community.particle.io/t/initialising-arrays-in-class-constructor/3295/2 "2014-03-11T20:10:16Z")

</div>

Great question Tim! If you know the initial values of the array at compile time:

- You should declare them once `static` for the class instead of once for every copy of the object.
- You should put it in flash with `const` to save on precious SRAM.
- You can just directly define the array values before object construction.

Example:

```auto
class LimitlessLED {
  private:
    static const byte white_ALL_off[2];
};

const byte LimitlessLED::white_ALL_off[] = { 0x39, 0x00 };

```

Hope that helps! If not, don’t hesitate to ask more questions.

Cheers!

---

<div class="post-metadata">

**Author:** ![timolol](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/timolol/32/2844_2.png) [@timolol](https://community.particle.io/u/timolol)\
**Post date:** [March 12, 2014, 12:09pm UTC](https://community.particle.io/t/initialising-arrays-in-class-constructor/3295/3 "2014-03-12T12:09:44Z")

</div>

Thank you for taking your time to answer my question, @zachary! 😄
