handle arrays from object

hello everybody
i'm wondering why doesn't this work. i just create an array in class declaration, and try to assign values to it in the object constructor...

1
2
3
4
5
6
7
8
9
10
11
12
13
class Foo{
  public:
  int numbers[4];
  Foo(){
    numbers = {1, 2, 3, 4,};
  }
  ~Foo();
};

int main(){
  Foo foo;
  return 0;
}


i get the error:
assigning to an array from an initializer list|


thanks in advance!
Hmm... I think it may be due to the extra comma in the list in the assignment.
you cannot use that kinda of initial value unless it is initialized when declared

This code bellow doesn't work
1
2
3
4
5
int main(){
    int n[4];
    n = {1,2,3,4};
    return 0;
}


so just initialized them

1
2
3
4
5
6
7
8
9
10
11
12
class Foo{
  public:
  int numbers[4];
  Foo() : numbers({1, 2, 3, 4}){
  }
};

int main(){
  Foo foo;
  return 0;
}
oh, i see. thanks for the help :)
i believe you can't initialize the array in the class declaration you have to do it in a function. also i don't believe you can't initialize it in the default constructor either.
Topic archived. No new replies allowed.