-3

I have a problem when I'm using a static variable in my class constructor. I need to set two values equal to zero on the first line of initialization in the constructor, but I don't want to have them set to zero after the constructor is called again. These two values will be incremented. So after finding out about static variables I tried to use static variables in my constructor but errors showed.

So, what I want is for horPos and vertPos to be set to zero only once in the constructor and then incremented in the other function.

IOMovement.cpp:

#include "stdafx.h"
#include <iostream>
#include <Windows.h>

#include "IOMovement.h" 
#include "rectangle.h"

#define W_KEY 0x57
#define S_KEY 0x53
#define A_KEY 0x41
#define D_KEY 0x44
#define R_KEY 0x52


void IOMovement::IO() {
    rectangle player(15, 5);

        if (GetAsyncKeyState(W_KEY)) {
            system("CLS");
            vertPos--;

            player.rectangleDrawPos(horPos, vertPos);
        }

        if (GetAsyncKeyState(S_KEY)) {
            system("CLS");
            vertPos++;
            std::cout << "Working\n";
            player.rectangleDrawPos(horPos, vertPos);
        }

        if (GetAsyncKeyState(A_KEY)) {
            system("CLS");
            horPos--;

            player.rectangleDrawPos(horPos, vertPos);
        }

        if (GetAsyncKeyState(D_KEY)) {
            system("CLS");
            horPos++;

            player.rectangleDrawPos(horPos, vertPos);
        }

}


void IOMovement::IOStartup() {
    //Variable decleration
    vertPos = 0;
    horPos = 0;

    //Functions
    IO();
}

IOMovement.h:

#ifndef IOMOVEMENT_H
#define IOMOVEMENT_H

class IOMovement {
    static int vertPos, horPos;

    void IO();

public:
    void IOStartup();
};



#endif
CryogenicNeo
  • 937
  • 12
  • 25
  • "but I don't want to have them set to zero after the constructor is called again." - what do you want to have them set to? And why do you think you need to do this? –  Apr 28 '18 at 23:47

3 Answers3

0

You do not need static for this use case. When you create an object, typically you call the constructor only once.

So, if you dont declare them as static and use the constructor to initialize them in the following way: IOMovement::IOMovement() : vertPos(0), horPos(0) {//Your constructor logic. }

0

Static variables are not initialized in constructor but maybe modified. They are initialized outside the class eg:

int IOMovement::vertPos = 0, horPos = 0;

And in des/constructor you can idec/ncrement them:

IOMovement::IOMovement(){
      vertPos++;
      horPos++;
}

IOMovement::~IOMovement(){
      vertPos--;
      horPos--;
}
Raindrop7
  • 3,889
  • 3
  • 16
  • 27
0

Actually, when you're designing a class you can define static variables (members) inside the class, but you can't initialize them inside the class, where they're defined. If you want to initialize a static class variable (member), you can do it outside the class, at the rest of the source file by this way:

IOMovement.h

#ifndef IOMOVEMENT_H
#define IOMOVEMENT_H

class IOMovement {
    // Here you define IOMovement class variables.
    static int vertPos, horPos;

public:
    // I think, you must put this function in the
    // public function memebers of the class.
    void IO();
    void IOStartup();
};

#endif

IOMovement.cpp

#include "IOMovement.h"

...
// Here you initialize both static variables, at the source file
// before you use them.
IOMovement::vertPos = 0;
IOMovement::horPos = 0;
...

At the constructor you only can initialize non-static class member variables.

In this page is explained better how are initialized static members of a class in c++:

All static data is initialized to zero when the first object is created, if no other initialization is present. We can't put it in the class definition but it can be initialized outside the class by redeclaring the static variable, using the scope resolution operator :: to identify which class it belongs to.

At this answer in Stack Overflow is a example:

They can't be initialised inside the class, but they can be initialised outside the class, in a source file:

// inside the class
class Thing {
    static string RE_ANY;
    static string RE_ANY_RELUCTANT;
};

// in the source file
string Thing::RE_ANY = "([^\\n]*)";
string Thing::RE_ANY_RELUCTANT = "([^\\n]*?)";

In these pages you can get more information about static member of a c++ class:

Static Members of a C++ Class

C++ initialize static variables in class?

Constructors (C++)

CryogenicNeo
  • 937
  • 12
  • 25