0

What I want is a class that contains objects and variables that can be accessed by any other class without needing to create an object of that class or get a reference to that object from the class that creates it. I know that in Java a could just create a static class that would do this but it doesn't seem like static classes exist in C++. How can I accomplish this?

Let me clarify further exactly what I'm looking for.
Say I have a class called "mob" which on update, seeks out the player and moves toward him. I could do this by putting a reference to the player in the update function of the mob, or better yet just create a pointer in the mob class that is assigned to a reference of the player on initialization. However, what I really want is to just be able to include a header like "Global.h" and put the player in there. Then when the mob, or any other object in the program wants to get some data from player, it can just use something like Global.player.GetPos()

Or maybe this is terrible practice that I should forget about right now. In which case, please tell me a better way to do this whole thing. Thanks.

JakeP
  • 327
  • 3
  • 14
  • Sounds like you want a singleton. Remember a singleton is just a pretty global variable, and the same design considerations about global variables apply. – Neil Kirk Nov 16 '14 at 05:56
  • But don't I still have to declare a singleton before using it? Like I have to say "Singleton single;" and then use "single.someVariable" to access it. I want to be able to use "Singleton.someVarialbe." – JakeP Nov 16 '14 at 06:00
  • There are many different types. There could be a static getter function, in which case it will be `Singleton::get().var` – Neil Kirk Nov 16 '14 at 06:01
  • I don't really understand this. – JakeP Nov 16 '14 at 06:03
  • http://stackoverflow.com/questions/1008019/c-singleton-design-pattern – Neil Kirk Nov 16 '14 at 06:04
  • 1
    There's a lot of debate about singletons, and whether they're good or bad depends a lot about how you end up using them. I wrote an article about it: http://www.embeddedrelated.com/showarticle/691.php – Jason S Nov 16 '14 at 14:15

1 Answers1

1

You could ether use a singleton pattern (you can find those alot everywhere, like Neil Kirk mentioned in his comment above), or, if you don't need object-oriented mechanics (like inheritance or polymorphy), I personally grew accustomed to just using simple functions instead of a class.

Mob.hpp

#ifndef MOB_HPP
#define MOB_HPP

namespace Mob {

// "public" functions and variables
extern int someVariable;
extern void moveTowards(Player* player);
extern void retreat(void);

}

#endif

Mob.cpp

#include "Mob.hpp"

// "private" functions and variables
static long _lastUpdate;
static void seekOut(Player* player) {
    //...
}

// "public" function implementation and variables
namespace Mob {
    int someVariable = 0;

    void moveTowards(Player* player) {
        //...
    }

    void retreat(void) {
        //...
    }
}

Usage

#include "mob.hpp"

int var = Mob::someVariable;
Mob::moveTowards(player);

The header basically defines all the public methods you would normally have in your static class. These are marked as extern so that they have external linkage and can be used in other compilation units.

In the .cpp file there some additional things marked as static. When you declare global functions or variables as static, they have internal linkage and are only visible in the unit where they are defined. So in a sense, they are private.

Some see this approach as being kinda backwards, but to be honest, when dealing with singletons without the need to actually use them as a class somehow, or get a pointer to the singleton instance, then functions are just more simple. One drawback though: If you want the mob not to be a singleton anymore, but an instanceable object, you'd have to write a class later on.

PuerNoctis
  • 1,364
  • 1
  • 15
  • 34
  • @NeilKirk `extern` is a valid storage specifier for functions, too. It is redundant because in the above case it has external linkage by default, yes, but it's not invalid. – PuerNoctis Nov 16 '14 at 13:47