How to create a global object/instance of a class?

Pages: 12
Hi, I'm new in object oriented programming.
I need help creating a global object/instance of a class, that can be used by any function.
Does anyone knows a solution?
Please feel free to write it.
Define it outside any function in some namespace including the global namespace.
closed account (S6k9GNh0)
If the global can be used from any function, be sure that the functions:

a) Cannot be used at the same time OR
b) Each function tries to lock a mutex so other functions will fail on attempt, thus not resulting in a race condition.
give me an example, please.
I tried it like this:
1
2
class Pacman;
Pacman player;

but it didn't work. ;(
Pacman is an incomplete type because you didn't use curly braces to define it.
This follows later:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Pacman{
public:
    Pacman(){
    pos[0] = rand()%3;
    pos[1] = rand()%3;
    pos[2] = 0;
    dir = 0;
    birth = t;
}pacman[2];
    ~Pacman();
    void draw();
private:
    float pos[3];
    char dir;
    float birth;
};
There are certain things you cannot do without the full definition of the class.
1
2
3
4
5
6
7
8
9
10
11
class Example;

Example e1; //ERROR
Example *ep1; //fine

class Example
{
};

Example e2; //fine
Example *ep2; //fine 
Ok. Thanks.
and how can I make a programm, eyerytime a key is pressed(or something like this) a new objekt/instance of a class is created, that can be used as global?
1. Global variables are bad
2. If you want to start dealing with key presses, you should move on to creating graphical windows and using a graphics library like SFML or SDL.
the problem aren't the keys.
the problem is, to create an undefined number of instances/objects of a class, that can be used by all functions in this programm.
Have you looked into std::vector?
Yeah, but haven't I use a different name for every instance/object I define?
If you want a different name for each one instead of a different number, try std::map<std::string, MyClass>. But this sounds like you're trying to replicate global variables and in a bad way. Can you be more precise about what you're trying to do?
I want to programm where every object is displayed as a dot. When you press a key( or some other event) a new dot will be displayed.
I know how to display and react on keys.
But I don't know how to manage this with classes.
Luk3 wrote:
every object is displayed as a dot. When you press a key( or some other event) a new dot will be displayed.
Be more specific. In a line? At certain places on a grid? Redisplayed every time?
redisplayed every time.
in a line or whatever doesn't matter, because the objects have their position and will by displayed by their on function: draw().
In this case you should be using something like SFML, you should not be using the console for this. ;)

Also, a std::vector will work fine for this. All you have to do is iterate the vector and call the draw() function.
Last edited on
Maybe like this?

1
2
3
4
5
class dot{
  draw{...};
  ...
  };
std::vector<dot *> dots;

to produce a new dot:
dots.push_back(new dot);
and to draw them:
dots[n]->draw();
Yes, except in this case you don't need the vector to be of pointers. It can just be std::vector<dot>.
Yeah, but when I want to produce and draw the dots in different functions I need to?
Pages: 12