seperating variables

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

Post Reply
Randi
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 50
Joined: Sat Apr 24, 2010 1:32 pm
Location: Noobville

seperating variables

Post by Randi »

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

Code: Select all

#include "variables.h"
x = 10;
User avatar
RyanPridgeon
Chaos Rift Maniac
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:

Re: seperating variables

Post by RyanPridgeon »

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
Chaos Rift Regular
Posts: 173
Joined: Thu Feb 11, 2010 9:46 pm

Re: seperating variables

Post by X Abstract X »

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.
User avatar
dandymcgee
ES Beta Backer
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:

Re: seperating variables

Post by dandymcgee »

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! :twisted:
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: seperating variables

Post by eatcomics »

Yep, my rewrite of my game is doing away with Globals altogether hopefully
Image
Randi
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 50
Joined: Sat Apr 24, 2010 1:32 pm
Location: Noobville

Re: seperating variables

Post by Randi »

thanks for the help! namespaces work like a charm.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: seperating variables

Post by avansc »

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"
Post Reply