3

I want to have a static class that would hold different kinds of data (like directories, values, etc.). I've tried using namespaces but namespaces do not allow member variables, which I need to hold data (correct me if I'm wrong, though).

What I'm trying to do is save some constants for the whole program to use - scalingFactor and screenSize to name a few. Then, I want those data to be shared by all parts of the program. Something like this:

In Foo.cpp:

void doSomething( float p_Float ) {
   printf( "Scaled Float is %.2f", p_Float * Globals.scalingFactor );
}

In Goo.cpp:

void doSomethingElse( ) {
   printf( "Scaling Factor is %.2f", Globals.scalingFactor );
}

The scalingFactor should refer to the same value, the value of scalingFactor in the global static class Globals.

Thanks in advance.

EDIT: I also need to use global functions, does it work the same as variables in this question?

3 Answers 3

9

So just have it as a static member:

//Globals.h
class Globals
{ 
public:
   static float scalingFactor;
}

initialize it in a single implementation file:

//Globals.cpp
#include "Globals.h"
float Globals::scalingFactor = 42.0;

and use the scope resolution operator to access it:

float x = Globals::scalingFactor;

I've tried using namespaces but namespaces do not allow member variables

You haven't used namespaces correctly. The following should work:

declaration in header:

namespace Globals
{
    extern float scalingFactor;
}

and the definition:

//Globals.cpp
namespace Globals
{
    float scalingFactor = 42.0;
}
10
  • With that, would I have to make an instance of the Globals class whenever I need to use it? (Was reading on static variables a few minutes ago)
    – alxcyl
    Aug 17, 2012 at 2:48
  • 1
    @LanceGray no. You don't need an instance. That's how static members work. Aug 17, 2012 at 2:50
  • Thanks. Let me try that and see if it fits. I was using static instead of extern in the namespace so that's probably why it didn't work.
    – alxcyl
    Aug 17, 2012 at 2:53
  • 1
    @LanceGray when used outside a class type, static gives a variable internal linkeage. That means a copy is created for every translation unit that uses it, so it's not a global. Aug 17, 2012 at 2:56
  • 1
    @LanceGray well... reference means something else (look it up). extern declares the variable, doesn't define it yet (you define it in the cpp file). Aug 17, 2012 at 3:04
6

C++ doesn't have static classes. Just classes. But this sounds like a perfect job for a namespace. Yes, you can put data in namespaces. Write a declaration in a header file, with extern for the data;

// program_data.h:
namespace program_data {
extern float scalingFactor;
extern int screenSize;
} // namespace program_data

and in one source file, provide definitions for the shared data:

// program_data.cpp:
#include "program_data.h"
namespace program_data {
float scalingFactor = 13.765; // whatever...
int screenSize = 640;
} // namespace program_data

and wherever you need to use these things, just refer to them with their namespace-qualified names:

// foo.cpp:
#include "program_data.h"
void doSomething( float p_Float ) {
   printf( "Scaled Float is %.2f", p_Float * program_data::scalingFactor );
}

And, yes, strict OOP doesn't do this kind of thing. But passing global data into every function, whether it needs it or not, imposes a great deal of overhead. This is known as "tramp data", and often reflects a design error. If these things are truly global, then they're global, and pretending that they aren't, in order to make your code "OOP", is a design error.

1
  • +1 for the last 3 sentences: too many people advocate jumping through hoops to achieve the same thing in an obfuscated way, merely to avoid being implicated in things that have a dogmatic reputation as being Bad Patterns. I'd also add that C++ has never pretended to be "strict OOP", and that's a good thing. :) Apr 11, 2016 at 13:36
2

Since you've tagged your question , I assume you are designing an object-oriented system. If so, I would suggest against using global variables. Consider creating meaningful classes for the data you are trying to pass around, and then pass objects around instead of accessing global variables.

For example:

class Bar {
private:
  float scalingFactor;

public:
  float getScalingFactor() {
    return scalingFactor;
  }
}

class Foo {
private:
  Bar &bar;

public:
  Foo(Bar &bar) : bar(bar) {
  }

  void doSomething( float p_Float ) {
    printf( "Scaled Float is %.2f", p_Float * bar.getScalingFactor() );
  }
}

class Goo {
private:
  Bar &bar;

public:
  Goo(Bar &bar) : bar(bar) {
  }

  void doSomethingElse( ) {
    printf( "Scaling Factor is %.2f", bar.getScalingFactor() );
  }
}

And at the place where you create Foo and Goo, you can pass the same instance of Bar to both:

Bar bar;

Foo foo(bar);
foo.doSomething(10);

Goo goo(bar);
goo.doSomethingElse();
5
  • Thanks, I will keep that in mind. Plus, this one seems more readable to me. But can you explain (or link to an explanation) of this syntax: Foo( Bar &bar) : bar( bar ) { } It looks like a constructor but the : bar( bar ) confuses me.
    – alxcyl
    Aug 17, 2012 at 3:30
  • @LanceGray: Yes, it is indeed a constructor. The part after the : is an initializer that initializes member variables. Maybe this makes more sense: Foo(Bar &x) : bar(x). You can read more about it here: cprogramming.com/tutorial/initialization-lists-c++.html
    – casablanca
    Aug 17, 2012 at 4:04
  • Does it work if I have a constructor that accepts other parameters? Like Foo( Bar &x, int i ) : bar( x ) or I need to have another constructor that accepts just that int (Foo( int i ))
    – alxcyl
    Aug 17, 2012 at 4:38
  • 1
    @LanceGray: Yes, you can have as many parameters as you want, and you can initialize them either with an initializer list or with a regular assignment inside the constructor, or a combination of both.
    – casablanca
    Aug 17, 2012 at 4:51
  • @Casablanca Of course, where we can use the initialiser list, we should, seeing as this is better style and sometimes essential, for example it's the only way to initialise a const member variable. Apr 8, 2016 at 14:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.