text: Q:
  
  Best way to use a C++ style api from a C style file compiled in C++
  
  I am working a in a Telecom company. We have some code generation tools that generate C style files based on a MIB definition. We compile them with C++ though.  It boils down to the fact that these generated files just contain a bunch of empty functions and that we have to implement them. Needless to say that this code generation is a one time thing :-)
  Now for the remainder we do our design in C++ and we use dependency injection and the like. Well at least I do and it is kinda poor mans DI. We handcraft everything and don't use a DI container.
  The problem here is that no matter how fancy your C++ design is, how can you call it from such a C style function in a graceful way ?
  I can think of a few mechanisms out of my sleeve :
  
  Use a singleton as representation to the C++ design, just meant as en entry point from the C-style to the C++ functionality
  Use a global. Kinda similar to the above because a Singleton really is a global. Therefore I don't like these 2 approaches. I learnt to have a love/hate relationship with Singletons. I see too many of them and avoid using them myself.
  Use a static getter somewhere in a Class that as such is an entry point to the C++ functionality. This is actually kind of a global access too. This getter can provide access to a C++ file scoped static variable for instance. 
  Use a proxy that you instantiate in the function body to proxy the other C++ functionality. But this just kinda shifts the problem because you can still not do any dependency injection. This way of thinking is more the direction I think I have to go. 
  
  You can assume that the C++ functionality is already initialized at the time that the 'C' style functions have to do their work. The dependency injection stuff is already done during initialization so I don't think the C style functions should worry about that. They just need an entry point to the C++ api.
  I hope I was able to make myself a bit clear ;-)
  
  A:
  
  Many C api's allow this to work by including a context pointer. This is typically of type void * and can point to anything. Simple cast it to the appropriate type, and away you go.
  If the system you are working with does not include a context pointer, you have no choice but to put to use a global. At that, point I say that simplicity is best, and you should just have a global variable somewhere.
  
  