0

I know there is many question talks about static function and variable but I can't find the one that explain me how do things like this:

board.h

 class board:public QGraphicsPixmapItem 
{
public:
    board();
    static basedice *gamepos[8][8];
};

and I want to defined my array like this:

board.cpp

board::board()
{
    for (int i=0;i<8;i++)
    {
        for (int j=0;j<8;j++)
        {
        gamepos[i][j]=NULL;
        }
    }

}

And I have one more question,Is that a right way to use an array in many classes something like global array... for example in chess game for holding postion of my pieces? Sorry for my bad english.

8
  • 1
    I think you should look at this: learncpp.com/cpp-tutorial/811-static-member-variables it seems you don't understand well how static member of class work Commented Sep 5, 2014 at 15:10
  • I saw that before but I can't see how do I define it in cpp file Commented Sep 5, 2014 at 15:16
  • 1
    If you think about Chess, then every board has its own place to hold the Chess pieces -> it would probably make more sense to play a game of Chess per board and let every board own the place where the positions are stored (i.e. don't have static gamepos, instead make it a member of class board) Commented Sep 5, 2014 at 15:16
  • Why I shouldn't use static ,I think It's easier to use. Is there any problem in using static? Commented Sep 5, 2014 at 15:26
  • 1
    Because in your particular case, 1 board => 1 gamepos array. If you make gamepos static then all the instances of board class share the same gamepos array. Commented Sep 5, 2014 at 15:31

2 Answers 2

1

If you really want the gamepos array to be static you can declare a static method in class Board that will initialize the array.

Then you call this method from outside the class.

int main() {

    Board * myboard = new Board();
    Board::initGamepos();

}

However looking at your code and what you want to do (which is reinitialize the gamepos array everytime you create a new Board instance, it is clear that you do NOT want gamepos to be static. 1 board <=> 1 gamepos array : that is not the mark of a static member, that is the mark of a standard member.

Sign up to request clarification or add additional context in comments.

2 Comments

Oh.Sorry I use an wrong example.. I don't want to initialize it every time.. I shouldn't put that in constructor and I don't know why I didn't try it before...
Research "c++ singleton". Perhaps you want to make the board a Singleton.
0

Static variables are automatically initialized to zero/false/null, so you don't need to initialize the array.

Anyway, you should not be reinitializing a static variable from your instance constructor as that will produce funny results.

6 Comments

Isn't reinitialization an Oxymoron?
Ok but how can I initialize it?
You don't need to initialize it, it is by default initialized to NULL in your case. If you want to initialize the static member to another value, you can make a static method to do so.
I have a question : Where is basedice declared, and where is it defined in this example, to me, it seems like basedice is not defined
@ user3670482 It's one of my classes that all of pieces inherits from it
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.