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!

2
  • 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 before coding any further. Commented Feb 5, 2015 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! Commented Feb 5, 2015 at 21:28

2 Answers 2

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 ) { ... }
Sign up to request clarification or add additional context in comments.

Comments

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.

5 Comments

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.
@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.
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?
Instead of doing it by inheritance, you could still do it with templates. Then some sugary typedefs. template<int num1, int num2> class WithSomeArray; typedef WithSomeArray<5,5> ship; typedef WithSomeArray<10,5> Bigship;
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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.