Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.
Moderator: Coders of Rage
Randi
Chaos Rift Cool Newbie
Posts: 50 Joined: Sat Apr 24, 2010 1:32 pm
Location: Noobville
Post
by Randi » Sat May 15, 2010 9:29 pm
I want to separate my declaration of variables outside of my source document, is there a way of doing this that does not declare them as global, I know that I can do it by doing something like the following, but to my understanding doing this declares them as globals.
main
Code: Select all
#include "variables.h"
int main()
{
printf("x is at %d", x);
}
variables.h
Code: Select all
include "variables.cpp"
extern int x;
variables.cpp
RyanPridgeon
Chaos Rift Maniac
Posts: 447 Joined: Sun Sep 21, 2008 1:34 pm
Current Project: "Triangle"
Favorite Gaming Platforms: PC
Programming Language of Choice: C/C++
Location: UK
Contact:
Post
by RyanPridgeon » Sat May 15, 2010 10:22 pm
main
Code: Select all
#include "variables.h"
int main()
{
printf("x is at %d", x);
}
variables.h
Code: Select all
#ifndef VARIABLES_H
#define VARIABLES_H
extern int x;
#endif
variables.cpp
Code: Select all
#include "variables.h"
int x = 10;
If you don't want it global, you will have to pass it through functions and/or use classes and OOP.
Ryan Pridgeon
C ,
C++ ,
C# ,
Java ,
ActionScript 3 ,
HaXe ,
PHP ,
VB.Net ,
Pascal
Music | Blog
X Abstract X
Chaos Rift Regular
Posts: 173 Joined: Thu Feb 11, 2010 9:46 pm
Post
by X Abstract X » Sat May 15, 2010 11:10 pm
Use a namespace if you don't want to put it in a class.
Globals.h
Code: Select all
#ifndef GLOBALS_H
#define GLOBALS_H
namespace Globals {
const static double PI = 3.14159;
}
#endif
Main.cpp
Code: Select all
#include "Globals.h"
std::cout << 'PI = " << Globals::PI << "\n";
Last edited by
X Abstract X on Mon May 17, 2010 4:07 pm, edited 1 time in total.
dandymcgee
ES Beta Backer
Posts: 4709 Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:
Post
by dandymcgee » Sun May 16, 2010 9:55 am
I saw this and immediately thought of Calculus.
A namespace seems like the best solution short of just making a class.
Falco Girgis wrote: It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!
eatcomics
ES Beta Backer
Posts: 2528 Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois
Post
by eatcomics » Sun May 16, 2010 12:19 pm
Yep, my rewrite of my game is doing away with Globals altogether hopefully
Randi
Chaos Rift Cool Newbie
Posts: 50 Joined: Sat Apr 24, 2010 1:32 pm
Location: Noobville
Post
by Randi » Mon May 17, 2010 2:43 pm
thanks for the help! namespaces work like a charm.
avansc
Respected Programmer
Posts: 1708 Joined: Sun Nov 02, 2008 6:29 pm
Post
by avansc » Mon May 17, 2010 2:50 pm
randi, look up what the variable qualifier "static" does, it may be something of interest.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"