I have got a difficult problem with "overriding" static arrays. I have got static arrays (for simplicity) which are of fixed length in various derived classes, but nonetheless all dimensions are known in compile-time. I have also got an online function within the base class, but I'm not sure how you can tackle the issue of overriding these arrays and array-dimensions within the derived classes to ensure that this virtual function works properly, i.e. provides the dimensions and array contents in the derived classes. Example:

class B  {
private:
 // these two are not really meaningful in this class (ABC)
 static const int n = 1;
 static double da[n];
public:

 virtual void f()
 {
  for(int i = 0; i < n; ++i)
  {
   // do something with n and da
   std::cout << n << std::endl;
  }
 }
};

class D1 : public B  {
private:
 // these are subclass-specific (i.e. n might also change)
 static const int n = 4;
 static double da[n];
};

class D2 : public B  {
private:
 // these are subclass-specific (i.e. n might also change)
 static const int n = 6;
 static double da[n];
};


double D1::da[] = {1., 2., 3., 4.};
// ...

int main()
{
 B *p = new D;
 p->f(); // I'd like to see 4 instead of 1
}

Would you advise a fix or an alternative way to do this properly? I believe std::vector would get it done (?), but needs much work to adjust to my existing code. Thank you, Peter

Since f is just defined within the Base class, it'll use that class' n variable.

Possibly move:

for(int i = 0; i < n; ++i) 
  { 
   // do something with n and da 
   std::cout << n << std::endl; 
  }

To some separate function, but rather get it which means you pass with what "n" is. like

void doStuff(int num) {
    for(int i = 0; i < num; ++i) 
    { 
      // do something with n and da 
      std::cout << num << std::endl; 
    }
}

And also have each subclass implement f() to call doStuff(n) where n is going to be each class' own n variable.

You could utilize the Strangely enough Recurring Template Pattern to create your function access the static array within the most-derived class.