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

Pages: 12
Aug 25, 2013 at 2:54pm
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.
Aug 25, 2013 at 3:58pm
Define it outside any function in some namespace including the global namespace.
Aug 25, 2013 at 4:08pm
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.
Aug 26, 2013 at 6:15pm
give me an example, please.
I tried it like this:
1
2
class Pacman;
Pacman player;

but it didn't work. ;(
Aug 26, 2013 at 6:27pm
Pacman is an incomplete type because you didn't use curly braces to define it.
Aug 27, 2013 at 5:09pm
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;
};
Aug 27, 2013 at 5:15pm
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 
Aug 27, 2013 at 5:30pm
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?
Aug 27, 2013 at 5:38pm
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.
Aug 27, 2013 at 5:46pm
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.
Aug 27, 2013 at 5:50pm
Have you looked into std::vector?
Aug 27, 2013 at 5:53pm
Yeah, but haven't I use a different name for every instance/object I define?
Aug 27, 2013 at 5:55pm
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?
Aug 27, 2013 at 6:00pm
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.
Aug 27, 2013 at 6:05pm
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?
Aug 27, 2013 at 6:08pm
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().
Aug 27, 2013 at 6:13pm
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 Aug 27, 2013 at 6:13pm
Aug 27, 2013 at 6:15pm
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();
Aug 27, 2013 at 6:17pm
Yes, except in this case you don't need the vector to be of pointers. It can just be std::vector<dot>.
Aug 27, 2013 at 6:19pm
Yeah, but when I want to produce and draw the dots in different functions I need to?
Pages: 12