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
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. }
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--;
}
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++)
Related
I create a basic IBasic interface with a static field
class IBasic
{
public:
IBasic();
virtual ~IBasic();
static std::vector< std::vector<char> > Field;
};
from which the Inherit class is inherited:
class Inherit : public IBasic
{
public:
Inherit(int);
~Inherit();
void Foo();
};
The Inherit class makes some manipulations with Field static member in constructor/or member function.
In order to create an instance of the Inherit class, we need to explicitly declare a static field in the main.cpp before the main function:
#include "Basic.h"
#include "Inherit.h"
std::vector< std::vector<char> > IBasic::Field;
int main()
{
Inherit(10);
return 0;
}
The questions are:
In what namespace does the static method actually exists (global?)? Because I know that static field/function is not a class member in fact.
Is there another way to declare this static method, for example, in a
class file, inside a main function, or through creation unnamed namespace? Is it only one right variant?
How is right? What should be considered first of all?
A static member of a class is a member of its class (that's a tautology) and its class namespace (a class is a namespace). It is not a nember of any other namespace.
A non-const static data member of a class must be defined exactly once in a program, outside of any class, in the same namespace its class is defined in (a global namespace in your case). A header file is inappropriate place for such declaration. It is normally placed in an implementation .cpp file that goes together with the header file.
Having said that, an interface should not have any static data members, much less public ones. It is most likely a grave design error.
In what namespace does the static method actually exists (global?)? Because I know that static field/function is not a class member in fact.
It is declared in scope of the class. In fact the static variable is a class member, your assumption is wrong.
Is there another way to declare this static method, for example, in a class file, inside a main function, or through creation unnamed namespace? Is it only one right variant?
The usual way is to define it in the translation unit that contains the function definitions for the class.
How is right? What should be considered first of all?
There's no right or wrong way, but as mentioned definition in the same translation unit as the class function definitions is the usual way.
Here's an example usage of a static member without any inheritance.
SomeClass.h
#ifndef SOME_CLASS_H
#define SOME_CLASS_H
class SomeClass {
private:
int x;
public:
static SomeClass* const get(); // Needed For Using class to get this pointer
SomeClass();
int getX() const { return x; }
void setX( int val ) { x = val; }
};
#endif // SOME_CLASS_H
SomeClass.cpp
#include "SomeClass.h"
static SomeClass* s_pSomeClass = nullptr;
SomeClass::SomeClass() {
s_pSomeClass = this;
}
SomeClass* const SomeClass::get() {
if ( nullptr == s_pSomeClass ) {
// throw exception
}
return s_pSomeClass;
}
Another class using above class as a static member
OtherClass.h
#ifndef OTHER_CLASS_H
#define OTHER_CLASS_H
class SomeClass; // Forward Declaration
class OtherClass {
private:
static SomeClass* pSomeClass; // The Static Member to this class
int y;
public:
OtherClass();
int getY() const { return y; }
void setY( int val ) { y = val; }
void useSomeClassToSetY();
};
#endif // OTHER_CLASS_H
OtherClass.cpp
#include "OtherClass.h"
#include "SomeClass.h"
SomeClass* OtherClass::pSomeClass = nullptr;
OtherClass::OtherClass() {
if ( nullptr == pSomeClass ) {
pSomeClass = SomeClass::get();
}
}
void OtherClass::useSomeClassToSetY() {
// First Set X To Some Value:
pSomeClass->setX( 10 ); // Use of Static Member
y = pSomeClass->getX(); // Use of Static Member
}
Static members still belong to the class, but they have static storage.
I have a basic class and header set up relating to an animal, I am trying to have a function which displays an animals current hunger level. Upon testing i have found the hunger level is not at the initial value i have set.
Monkey.cpp
#include "Monkey.hpp"
using namespace std;
Monkey::Monkey() {
}
unsigned int foodEaten = 0;
unsigned int foodIntake = 800;
unsigned int Monkey::hungerLevel() const {
return (makefoodIntake - foodEaten);
}
Monkey.hpp
#ifndef MONKEY_HPP
#define MONKEY_HPP
class Monkey : public Animal {
private:
unsigned int foodEaten;
unsigned int foodIntake;
public:
Monkey();
// Overridden to implement the monkey's hunger level
virtual unsigned int hungerLevel() const;
#endif /* end of include guard: MONKEY_HPP */
Why does this not return 800 initially?
Because you did not initialize the members of the class in your constructor.
You have some other variables, with the same name that are declared in the global scope.
However, because you have class members of the same name, the method uses the class members, and not the variables in the global scope.
You need to simply initialize the class members, instead of declaring global variables of the same name:
Monkey::Monkey() : foodEaten(0), foodIntake(800)
}
Both, foodEaten and foodIntake defined in the .cpp file are two variables in the global scope and not members of the class Monkey.
Initialize them in the header file as well:
class Monkey {
private:
unsigned int foodEaten{0};
unsigned int foodIntake{800};
// ...
};
/*
* CDummy.h
*/
#ifndef CDUMMY_H_
#define CDUMMY_H_
class CDummy {
public:
CDummy();
virtual ~CDummy();
};
#endif /* CDUMMY_H_ */
I've read that one should not declare class variables in header files. ist this right?
So I declare it in the cpp file below:
/*
* CDummy.cpp
*/
#include "CDummy.h"
static int counter = 0; //so here is my static counter. is this now private or public? how can i make it public, i cannot introduce a public block here.
CDummy::CDummy() {
counter++;
}
CDummy::~CDummy() {
counter--;
}
using this code i cannot access the classvariable from my main program....
thx
A "class variable" needs to belong to a class. So it has to be declared in the class definition. If the class definition is in a header file, then it follows that the class variable declaration must also be in a header file.
The class variable's definition should go in an implementation file, typically the one in which the class' members are defined. Here's a simplified example:
Foo.h
struct Foo
{
void foo() const;
static int FOO; // declaration
};
Foo.cpp
void Foo::foo() {}
int Foo::FOO = 42; // definition
What you have here:
static int counter = 0;
is a static variable that is not a static member of any class. It is only non-member static variable, static to the compilation unit of CDummy.cpp.
static int counter = 0; //so here is my static counter. is this now private or public? how can i make it public, i cannot introduce a public block here.
From the code I see counter is just a global static variables as it is not declated anywhere in your CDummy
Static variables should be public so that you could initialize them outside the class declaration. Your code should look like this to have it public:
class CDummy {
public:
static int count;
CDummy();
virtual ~CDummy();
};
// inside CDummy.cpp
int CDummy::count = 0;
Here you can read more about how to use static variables in class declaration.
I am currently doing a checker for the top level domains of email addresses. In order to check, I am comparing it to a list that is a text file. I want to import the list into a static map container. However, when I try to instantiate it, it says that it cannot be defined in the current scope. Why is that?
This is some my header file:
class TldPart {
public:
static void LoadTlds();
private:
static map<string,bool> Tld;
}
Here is the implementation in the cpp:
void TldPart::LoadTlds()
{
map<string,bool> Tld;
...
}
It is telling me that ValidTld cannot be defined in the LoadTlds function.
Static members of a class exist outside of an object. You should define and initialize static member outside of the class.
Here we define and initialize a static class member:
header-file:
#pragma once
#include <map>
#include <string>
class TldPart {
public:
static void LoadTlds();
private:
static std::map<std::string, bool> tld;
};
your cpp-file:
#include "external.h"
std::map<std::string,bool> TldPart::tld;
void TldPart::LoadTlds()
{
tld.insert(std::make_pair("XX", true));
}
And don't forget semicolon at the end of the class.
EDIT: You can provide in-class initializers for static members of const integral type or static members that are constexprs and has literal type.
I am trying to implement the following class. However, when I try to instantiate an object of the class within its definition and pass "0" as value to initialize the object, i get an error:
"a type specifier is expected".
Can anyone explain how can i remove this error?
class MessageType
{
public:
static const MessageType msgEvent(0);
private:
MessageType();
virtual ~MessageType();
MessageType(int);
};
You need to initialize(define) it outside the class definition in a cpp file.
MessageType const MessageType::msgEvent;
However, Your intent in doing so is not very clear. Are you trying to implement a Singleton Pattern, probably this sample implementation might help, I leave it to you to decide, whether you really need a singleton, inspite of its disadvantages:
//MessageType.h
#include <boost/noncopyable.hpp>
class MessageType: private boost::noncopyable
{
public:
static MessageType* instance();
private:
MessageType();
~MessageType();
static bool g_initialised;
// static initialisation
static MessageType g_instance;
// dynamic initialisation
};
// MessageType.cpp
#include "MessageType.hpp"
#include <ostream>
#include <iostream>
#include <cstring>
bool MessageType::g_initialised;
// static initialisation
MessageType MessageType::g_instance;
// dynamic initialisation
MessageType::MessageType()
{
g_initialised = true;
}
MessageType::~MessageType()
{
g_initialised = false;
}
MessageType* MessageType::instance()
{
return g_initialised ? &g_instance : 0;
}
You can only initialize static member variables in the definition if they are of int type.
class MessageType
{
public:
static int sCount = 0; // That is fine.
static std::string sLogName; // That is fine.
static std::string sLogName("log.txt"); // Fail!
};
There's no way around this rule. If you want to initialize a static member variable, then you have to do it in the cpp:
std::string MessageType::sLogName("log.txt"); // fine, in the cpp.
This same rule applies directly to your MessageType instance, and has nothing to do with the fact that the class is of it's own type.