C++ initialize member array

791 views Asked by At

I am trying to create a class (foo) with a private member array. This class will be used as a private member of another class (foo2) in the constructor of which the array will be initialized.

example.h:

class foo{
private:
    int* ary;
public:
    foo(int*);
    ~foo();
}

example.cpp:

foo::foo(int* b){
ary = b;
}

useOfExample.h

class foo2{
private:
  foo my_foo;
public:
  foo2();
  ~foo2();
}

useOfExample.cpp

foo2::foo2() : myfoo({2,3}){}

I am kind of a noob in C++ and I realize what I am asking may not be really clear so in other words I need foo2 to have a member foo of which the array will be set to [2,3].

1

There are 1 answers

1
user On BEST ANSWER

Take a look at: Static array vs. dynamic array in C++

You cannot use the {...} syntax to initialize a dynamic array; you need to fill it up manually.