0
class ship
{
private:
    static const int num1 = 5;
    static const int num2 = 5;
protected:
    virtual int getNum1(){
        return num1;
    }
    int mainArray[getNum1()][num2];
}

basically I want to make a getter for a static const variable, this getter is later going to be used to initialize the array below it. how can i do this? (I am getting many errors with the current code above, it is just there to demonstrate my problem better). I want to be able to have a class that inherits from this class and change the size of the array using virtual getters. thanks in advance for any answers!

Matias Chara
  • 921
  • 6
  • 21
  • Impossible: array size should be known at compile time. Use dynamic memory allocation if it's not. More problems: 1) we tend to use library containers instead of plain arrays, 2) Most times you will not want to use multidimensional containers, but to use proper indexing on one-dimensional array instead 3) do you really need statics here? Looks like you are trying to write C with classes instead of C++. I would strongly recommend you to pick up a [C++ best practices book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) before coding any further. – Ivan Aksamentov - Drop Feb 05 '15 at 21:20
  • I might use dynamicly allocated arrays, they seem to be the solution now that I think about it. thanks for the reply! – Matias Chara Feb 05 '15 at 21:28

2 Answers2

3

Make your static const int public. As it is const noone can change it anyway and the compiler will understand...

class Ship
{
    public:
    static const int num2 = 5;
};
static float myArray[Ship::num2];

As it turned out in the comments section of the question, there is need to have flavours of ships which differ in the size of that internal array. Trying to override a virtual function to get the size, the array shall have won't work. Here a template based version where those classes can still be used in a polymorphic way.

class IShip
{ virtual void Sink() = 0; // and so on. Anything those classes have in common...
};
template<int xDim, int yDim>
class SomeShip
    : public IShip
{
    static const int num1 = xDim;
    static const int num2 = yDim;
    int mainArray[xDim][yDim];
public:
   // Implementation of IShip...
};

typedef SomeShip<5,5> Ship;
typedef SomeShip<10,5> Bigship;

Bigship myBigShip;
void HandleShip( IShip * ship ) { ... }
BitTickler
  • 10,905
  • 5
  • 32
  • 53
0

I don't think you can accomplish what you want - with a getter initializing the array. Why don't you just use the num1 there instead? Also, you can add the keyword static before int getNum1() - if you decide to keep that method anyway.

  • because I want to be able to have a class that inherits from this class and change the size of the array using virtual getters. – Matias Chara Feb 05 '15 at 20:58
  • 1
    @MatiasChara Your compiler will not accept the return value from a getter to initialize an array like that. Alternative: Take a std::vector and use resize() instead. – BitTickler Feb 05 '15 at 21:00
  • the only issue is I would rather not use vector (I would have to change lots of code), is there any way to work around this issue? – Matias Chara Feb 05 '15 at 21:03
  • Instead of doing it by inheritance, you could still do it with templates. Then some sugary typedefs. ``template class WithSomeArray; typedef WithSomeArray<5,5> ship; typedef WithSomeArray<10,5> Bigship;`` – BitTickler Feb 05 '15 at 21:11
  • I havent learned templates yet, I will learn them and then test out your idea. thanks for the reply and the comments! have a good day! – Matias Chara Feb 05 '15 at 21:18