0
class c{};

c *ar[2];

ar = {new c, new c}; //error

"Array type is not assignable" How to assign it after declaring it, i want to do that because the class is using that variable so i just want to declare it, create a class and then assign the value, i can't assign before the class because it can't make a new c without defing the class first.

2 Answers2

1

use standard library algorithm generate:

#include <algorithm>
#include <iterator>

class c{};
c* ar[2];
std::generate(std::begin(ar), std::end(ar), [] { return new c; });
Andriy Tylychko
  • 15,609
  • 5
  • 59
  • 110
0

use a loop:

for(auto i = std::begin(ar);i!=std::end(ar);++i)
    *i = new c;

this will work for (almost) all container classes, not just raw arrays.

IlBeldus
  • 1,030
  • 6
  • 14