I was wondering if it is possible to declare an array (size not known at this time), as a private member of a class and later set the size in the constructor of the class. For example:
class Test { int a[]; public: Test(int size); }; Test::Test(int size) { a[size]; // this is wrong, but what can i do here? }
Is this possible or should I use dynamic arrays? Thanks!
No this is not possible. Array declarations in headers must have constant sized value. Otherwise it’s impossible for constructs like ‘sizeof’ to function properly. You’ll need to declare the array as a pointer type and use new[] in the constructor. Example.