Skip to main content
gamedev.net
🔒 Locked

Help accessing static variable from multiple files.

Started by dustamulet 4 replies 550+ views
Original Post
dustamulet
dustamulet
Hi I have a problem trying to access a static variable contained in the namespace 'NameSpace'. I have 2 classes: IntializeStatic and AccessStatic. InitializeStatic is called first and in the output it gives the correct result. After this, AccessStatic is called and in the output gives an incorrect result. The output produces: Init: 100 Access: 0 When I want it to produce: Init: 100 Access: 100 Can anyone help?? main.cpp
#include <iostream>
#include "InitializeStatic.h"
#include "AccessStatic.h"
using namespace std;
using NameSpace::InitializeStatic;
using NameSpace::AccessStatic;

int main()
{
	InitializeStatic  init;
	AccessStatic      access;

	system( "PAUSE" );
	return 0;
}
InitializeStatic.h
#ifndef INITIALIZESTATIC_H
#define INITIALIZESTATIC_H

#include "AccessStatic.h"
#include <iostream>
using namespace std;
using NameSpace::AccessStatic;

namespace NameSpace
{
	class InitializeStatic
	{
	public:
		InitializeStatic();
	};
}

#endif
InitializeStatic.cpp
#include "InitializeStatic.h"
using NameSpace::InitializeStatic;

InitializeStatic::InitializeStatic()
{
	NameSpace::StaticInt = 100;

	cout << "Init: " << NameSpace::StaticInt << endl;
}
AccessStatic.h
#ifndef ACCESSSTATIC_H
#define ACCESSSTATIC_H

#include <iostream>
using namespace std;

namespace NameSpace
{
	static int StaticInt = 0;

	class AccessStatic
	{
	public:
		AccessStatic();
	};
}

#endif
AccessStatic.cpp
#include "AccessStatic.h"
using NameSpace::AccessStatic;

AccessStatic::AccessStatic()
{
	cout << "Access: " << NameSpace::StaticInt << endl;
}
JohnBolton
JohnBolton
A static (non-member) variable can only be accessed in the file it is defined in. If you have two static variables with the same name in different files, they are different variables. Since you have "static int StaticInt = 0" in the header file, you are defining a static variable named StaticInt in every file the header is included in. Since the variables are static, they are all different variables.

The bottom line is that you can only access a static (non-member) variable in the file it is defined in. So the question is this: What are you trying to do?
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
dustamulet
dustamulet
I wanted to keep the StaticInt variable accessible from anywhere by just typing in:

NameSpace::StaticInt;
yadango
yadango
hmmm for something like that wouldn't extern be a better keyword? you could also declare the static int in only one of the cpp files and have functions to access it.
dustamulet
dustamulet
Quote:
Original post by yadango
hmmm for something like that wouldn't extern be a better keyword? you could also declare the static int in only one of the cpp files and have functions to access it.

Thx I got it working from what you suggested:

1. Add "int NameSpace::StaticInt = 0;" to AccessStatic.cpp
2. Change "static int StaticInt = 0;" to "extern int StaticInt;" in AccessStatic.h

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.