Pass array size constant from main file to class

High-level code is relative. There's no reason not to abstract away memory management in its own class. The high-level class's memory layout and the performance will be identical to what you'd get if you called new directly in its constructor.
However, by abstracting it away, you eliminate an entire class of bugs. Which is important for this exact reason:

If you follow the rule of zero, you cannot miss it.

See also: C++ Core Guidelines: C.20: If you can avoid defining default operations, do

I know about the need to create (or disable) copy constructors and the assignment operator, but to be honest, this is the first time I've heard about the rule of zero. For example, I don't see where in the TestClass there are default operators that I should avoid.
Looks like I need to look into this theme more seriously.

On an AVR without access to the STL, that's really the only practical option.

You also need to check against failure of the allocation. On an AVR, a simple check against nullptr appears to be sufficient:

  ptr = reinterpret_cast<uint8_t *>(new uint8_t[5000]);
  if (ptr == nullptr) {
    Serial.println("Allocation Failed");
    while (1) {
    }
  }

However, I've found that to not always be the case with ARM / ESP. I'm told that's because in Big Computer C++, the new operator throws an exception on allocation failure rather than returning nullptr. Most Arduinos (including ARM / ESP) don't have exceptions enabled by default (if it's even possible at all). So, 'new' just returns. Problem is, the compiler / optimizer is smart enough to know that (in Big Computer C++), 'new' never returns nullptr. So, the null check is simply optimized away. Thus, I've taken to using:

#include <new>
uint8_t *ptr;

void setup() {
  ptr = reinterpret_cast<uint8_t *>(new(std::nothrow) uint8_t[5000]);
  if (ptr == nullptr) {
    Serial.println("Allocation Failed");
    while (1) {
    }
  }
}

Arduino is a great beginners tool to get projects started. What's a good way to start digging in to these more complicated concepts? There is a lot of information out there on objects and classes in C++. Where's a good place to get started? Should I be looking at developing my basic code skills in C++ before applying these more complicated concepts to Arduino? Arduino (and this forum) made things very easy to get started with the basics. Is there something similar for C++? Especially something like this forum. There seems to be an overwhelming amount of c++ forums, which is best?

read again the previous answers.
The solution would be a template.

if you insist on this precompiler define, create a .h with the define and include it in the main.ino sketch and your class.h

Post #13.

Thanks. Now I'm getting "invalid use of template-name 'TestClass' without an argument list"

Main.ino

#include "Test_class.h"

const int TEST_ARRAY_LENGTH = 800;

TestClass<TEST_ARRAY_LENGTH> TestObject800;

void setup() {
}

void loop() {
}

Test_class.h

#ifndef Test_class_h
#define Test_class_h
#include "Arduino.h"

template<size_t TEST_ARRAY_LENGTH>
class TestClass {
  public:
    float test_array[TEST_ARRAY_LENGTH];
};

#endif

Test_class.cpp

#include "Test_class.h"
#include "Arduino.h"

TestClass::TestClass(int TEST_ARRAY_LENGTH)
{
  test_array[TEST_ARRAY_LENGTH];
}

I tried this and got "'TEST_ARRAY_LENGTH' was not declared in this scope"

Test_class.cpp

#include "Test_class.h"
#include "Arduino.h"

TestClass<TEST_ARRAY_LENGTH>::TestClass(int TEST_ARRAY_LENGTH)
{
  test_array[TEST_ARRAY_LENGTH];
}

Template classes require the complete class definition to go the .h file or an #include(d) .hpp file.

I think I've found a solution. The declaration of the array size is no longer in the main file but I've written a function to access it instead.

Main.Ino

#include "Test_class.h"

TestClass TestObject;

void setup() {
}

void loop() {
}

Test_class.h

#ifndef Test_class_h
#define Test_class_h
#include "Arduino.h"

const int TEST_ARRAY_LENGTH = 800;

class TestClass {
  public:
    TestClass();
    int getArrayLength();
    
  private:
    float _testArray[TEST_ARRAY_LENGTH];
    int _arrayLength;
    
};

#endif

Test_class.cpp

#include "Test_class.h"
#include "Arduino.h"

TestClass::TestClass()
{
  _arrayLength = TEST_ARRAY_LENGTH;
  _testArray[TEST_ARRAY_LENGTH];
}

int TestClass::getArrayLength(){
  return _arrayLength;
}

Bad idea putting that in the .h file which gets #include(d) in both the .cpp and .ino files. You'll end up with a Multiple Definition linker error.

This is fine, const variables have internal linkage.

Edit: well, the definition of a constant in a header file is fine, the rest of the code is not.

Interesting. Please elucidate.

Ok. So this is not a good solution to this problem?

I would like to have the size of the array just defined in one place if possible.

PieterP you think the constant declaration is ok where it is but there's something else that's not right?

Constants are only visible inside of the current translation unit. Having the same constant defined in multiple translation units do not cause multiple definition errors.

This does not do what you think it does. It accesses the (nonexistent) element beyond the end of the array, which is not allowed.

Saving the size is not useful, because it is constant.

Oh yes! I see that now. Thank you. Is this the right way to do it?

float _testArray[TEST_ARRAY_LENGTH] = {};

Ok, now i see this one too. Thanks so much. This has been super helpful!

Is this looking ok?

Test_class.cpp

#include "Test_class.h"
#include "Arduino.h"

TestClass::TestClass()
{
  float _testArray[TEST_ARRAY_LENGTH] = {};
}

int TestClass::getArrayLength(){
  return TEST_ARRAY_LENGTH;
}

This simply defines and initializes a local variable that's never used, which is probably not what you intended. It's not clear what you want it to do, though. If you want to change the size of the array from the sketch, why use a constant instead of the template approach suggested by gfvalvo?

Yes, great. I was just wanting to define and initialise the array with this line. The array will be accessed with other functions of the TestClass.