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

Pages: 12
No, you should have no problems. Can you give an example where you think it won't work?
I think you can just do this:
1
2
3
4
5
class dot{
  draw{...}
  ...
};
std::vector<dot> dots;


to create:
dots.push_back( dot() );
to draw:
dots[n].draw();
Or:
1
2
for (std::vector<dot>::iterator it = dots.begin(); it != dots.end(); ++it)
    it->draw();
@L B: So I don't need a vector of pointers?
@Stewbond: I did this.
Thank you very much.
It works. :)
Last edited on
Yes, and look more closely at Stewbond's example - he does not use pointers, as I suggested.
Yeah.
I changed it.
closed account (S6k9GNh0)
Globals really aren't as bad as some people make them out to be in the thread... you just have to be careful with it. Accessing a resource in a uniform location is often faster than passing it via a function or handler.

You just need to either use a mutex (or something to prevent data races) or use TLS. SFML uses TLS.
You say accessing globals is faster than passing by reference and then you proceed to suggest mutexes? There's multiple inconsistencies here ;)
closed account (S6k9GNh0)
Well, a mutex can often have no lock time at all. It's just the unlucky case of actually hitting the lock. Sometimes bad programming can cause the mutex case to be slow as well where you do things that unnecessary while keeping the mutex locked (like doing syncronous IO with a locked mutex = rather slow and probably wrong).

Although, you're also right. Just as bad practice can make this method just as bad though.
Last edited on
I am not following computerquip's point, mutex = you are wasting (n - 1)/n processor resource (n = the number of your processors) if you are not using it correctly, not only sync IO. and even you are using it correctly, the waste is also increasing after the processor count increased. this is the reason we have lots of lock free data structures, say queue, memory allocator, etc.
usually nothing different when trying a mutex queue or a lock free queue, if you have only 2 - 4 cores. but when you are trying to run it on a 32 cores machine, you will see the gap.
Topic archived. No new replies allowed.
Pages: 12