2

As a C++ beginner, I oftentimes find myself struggling with the declaration of class attributes inside the header file that need more information than just a name, like arrays and objects of other classes with constructors.

Here's an example

SomeClass.h :

#include "OtherClass.h"

class SomeClass {

  int num; // works fine
  float arr[]; // produces an error because size is not declared

  OtherClass obj; // produces an error because the constructor parameters are not passed in

public:
  void setup();
  void update();

};

SomeClass.cpp:

#include "SomeClass.h"

void SomeClass::setup() {
  num = 10; // easy peasy, works!
  arr = float some_arr[5 * num]; // error

  // Fill in the array
  for (int i = 0; i < 5 * num; i += num) {
    ass[i] = 12;
  }

  // Fill in the class attributes
  obj = {120, 40}; // error

}

void SomeClass::update() {
  // Update stuff
}

In case of the array arr, how can I declare an array, if I don't know it's size at the moment of the declaration in the header file?

How can class objects with constructors be declared in the header file, without passing in unknown parameters at that moment?

Thanks.

St4rb0y
  • 317
  • 3
  • 5
  • 22

2 Answers2

3

In case of the array arr, how can I declare an array, if I don't know it's size at the moment of the declaration in the header file?

You can't! C++ does not support variable length arrays, although some compilers (such as GCC) add support for them as an extension.

Instead, you should consider using the std::vector container type, from the Standard Template Library.

In your header/declaration:

class SomeClass {
    int num; // works fine
//  float arr[]; // produces an error because size is not declared
    std::vector<float> arr;
    //...
};

And, for your setup() function:

void SomeClass::setup() {
    num = 10; // easy peasy, works!
//  arr = float some_arr[5 * num]; // error
    arr.resize(5 * num); // Sets the size of the container
    // Fill in the array...
    for (int i = 0; i < 5 * num; i += num) {
        arr[i] = 12; // You can access (valid) elements just like a normal array!
    }
    //...
}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
1

In case you don't know the array size before hand you can use dynamic allocation feature in C++.

First declare your array variable as follow

float *arr;

Then you can allocate required size as follows

arr=new float[10]; 

To deallocate memory

delete[] arr;

If want to dynamically allocate objects then,Declare class as

ClassName *obj;

Then to allocate use

 obj=new ClassName(your_parameters);

Then you can delete it using

delete obj;

TIP: It is always a good practice make pointer variable NULL after you have de-allocated memory. Do arr=NULL; and obj=NULL; after de-allocating

  • 5
    If you're going to 'encourage' the use of raw pointers, then you should also point out the need for a `delete[] arr;` statement (in the destructor). – Adrian Mole Apr 26 '20 at 11:49
  • 1
    @BRUCE - Thanks, works like a charm! Do you know by any chance, if this also works for class objects (with constructor)? – St4rb0y Apr 26 '20 at 13:49
  • 1
    @St4rb0y -If you mean by dynamically allocation memory like I did in array I have edited answer check that out –  Apr 27 '20 at 12:35