Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ global structure creates name conflict

I have written a quite extensive framework that drives characters in a physical simulation. Even though everybody warned me not to do it, I used a global public data structure for storage of information and called it State. It's not in a namespace either. I make it globally accessible by declaring extern State state;. The reason why I did this is because this structure is needed everywhere in the application and I find it extremely convenient to just include my State.h and then write to state.var anywhere and read state.var anywhere. The framework is changing rapidly, too, and I also find comfort in not having to care about passing data around, synchronizing etc. when new components are introduced.

Anyhow, now the s*** hit the fan. I want to use one of Qt's GUI classes and it already has it's own member called state of type State. Their state is at least in a namespace, but it doesn't seem to matter, since inside the class I'm already using that namespace.

What can I do now?

like image 218
Marcell Avatar asked Dec 12 '22 20:12

Marcell


1 Answers

Your only choice is to rip out your global and replace it with something sane. This is extremely painful but you really don't have any other option. This is why people recommended against using one in the first place.

In short, congratulations on learning the lesson at hand- don't use global variables.

like image 54
Puppy Avatar answered Jan 01 '23 13:01

Puppy