0

So, I want to create an array as a class member variable using the size I get from some other class function.

The following code is within one of my header files

static const int arraySize = B::getInstance().getArraySize();

A* myArray[arraySize];

On trying to run this code, I get the following error: 'In-class initializer for static data member is not a constant expression'

Note: I am trying to do this with just regular arrays (if possible) instead of using something like vectors

Additional Details:

  1. B is a singleton class with the following method,
const B& B::getInstance() {
  static B instance;

  return instance;
}
  1. Signature of getArraySize() in class B is
const int B::getArraySize() const
  • 3
    `arraySize` needs to be a compile time constant not just a runtime constant. – Richard Critten Aug 22 '23 at 20:41
  • 1
    This is C++, so use std::vector or another std:: container. – Dave S Aug 22 '23 at 20:46
  • I am trying to do it with just regular arrays if possible – GAMERZNIGHTMARE Aug 22 '23 at 21:03
  • @RichardCritten how do I make `arraySize` a compile time constant if I need to rely on methods from another class for initialization – GAMERZNIGHTMARE Aug 22 '23 at 21:06
  • 1
    You give VERY little information, so suggestions are just shots in the dark ... that mentioned - if you can make your "getArraySize()" constexpr, then you _might_ be able to move on ... – kaba Aug 22 '23 at 21:38
  • 1
    If `B::getInstance().getArraySize()` isn't a compile-time constant, then you can't. The `arraySize` size must be known at compile-time. You must either use a std:: collection or use a pointer and new(). – Dave S Aug 22 '23 at 22:00
  • 1
    Does this answer your question? [ISO C90 forbids variable length array \[-Werror=vla\]](https://stackoverflow.com/questions/60922521/iso-c90-forbids-variable-length-array-werror-vla) – Den-Jason Aug 22 '23 at 22:11
  • "I am trying to do it with just regular arrays if possible" - you can't use VLAs since C90. – Den-Jason Aug 22 '23 at 22:12
  • @Den-Jason, I mean I don't want a variable length array. The `getArraySize()` will return a constant fixed size that I intend to use for initializing `myArray` – GAMERZNIGHTMARE Aug 22 '23 at 22:18
  • Is that a fixed constant known at *compile time* that is the same constant for every B object? If not, then you are asking the size to be set at runtime, and so it is a variable length array. – Dave S Aug 22 '23 at 22:19
  • @DaveS, yeah technically, there is function that computes the array size before `getArraySize()` is called to return this computed value. So, I am inadvertently creating a variable sized array, ain't I? (even though the computed size will always stay constant) – GAMERZNIGHTMARE Aug 22 '23 at 22:23
  • 1
    Yes, "stays constant" and "a constant known at compile time" are very different things. The compiler can't know that the result will always be the same, and even if the calculation could be resolved at runtime it might be too indirect for the compiler to detect that. – Dave S Aug 22 '23 at 22:40
  • Got it, thanks. While I don't know the exact number of elements, `myArray` will have, I know the upper limit. So, I will use that (instead of variable-sized vectors) – GAMERZNIGHTMARE Aug 22 '23 at 22:56

0 Answers0