+ 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
3 Answers
+ 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)
+ 5
Put "return 0" in the end of the function
+ 2
"return 0" is added automaticly and is not needed.