+ 1

C++ variable length Array? - Answered

Is there a way of making an array with a variable length? here is my attempt: class T { public: T(float c); float a[b]; }; int main() { T a = new T(5); return 0; } // why does this not work? T::T(float c) :b(c) {;} https://code.sololearn.com/ciO17r85TYE7/?ref=app

29th Apr 2019, 6:08 PM
Tim L.
Tim L. - avatar
3 Respostas
+ 3
I believe the size of classes/objects has to be known at compile time and so b can't be variable. The ways to make variable-length arrays are - using the `new` keyword: `new float[b];` - using std::vector (recommended)
30th Apr 2019, 1:33 AM
Schindlabua
Schindlabua - avatar
+ 5
Put "return 0" in the end of the function
29th Apr 2019, 6:25 PM
InvBoy [ :: FEDE :: ]
InvBoy [ :: FEDE :: ] - avatar
+ 2
"return 0" is added automaticly and is not needed.
29th Apr 2019, 6:46 PM
Tim L.
Tim L. - avatar