I am making a C++ glfw wrapper for myself to use. I want to have classes like Monitor
, Window
, Context
that would be wrappers for glfw objects like GLFWMonitor*
or GLFWWindow*
. The problem is that if I want to have a method create_window()
in the Window
class, it must have access to the underlying raw glfw pointer in Monitor
class. At the same time, if I wanted the Monitor
class to have a method like enumerate_windows()
, it would need access to the underlying glfw window pointer. I could have a public method get_raw_handle()
in both classes, but implementation details are supposed to be hidden and I don't want the user to be able to access this information. It should only be available to the library.
I thought that I could friend
the classes to each other, but that doesn't seem right, as if I wanted to add a new class, I would have to friend
all existing classes to it and the other way around. It would also require forward declarations for everything and right now my library is header only.
I also thought that maybe all these objects could inherit from some kind of super WindowingSystemObject
class that would have a protected
method get_raw_handle()
, but that would mean that the handle would need to be universal (like a void*
) and then cast to the appropriate type.
All this seems so complicated. Instead of writing code I feel like some kind of a philosopher. I don't know anymore if I should make it in C++ (with classes) or just make it in C and don't care about any encapsulation as long as it works.
GetGLFWMonitor
, and make it afriend
to any wrapper class that holdsGLFWMonitor
instances. Then, all methods inGetGLFWMonitor
class are protected, and available to a list of friend classes of your other wrappers that need that. This will localize the knowledge and friending going on.internal
keyword in C#). In fact, in C++ you also do not have the option to selectively export C++ definitions. Which means all C++ definitions are available to downstream C++ consumers of your library, which means there is no true internal visibility whatsoever.