There is no inherent necessity for a glfwCreateWindow()
to return a pointer-to-a window, rather than an actual window. The OpenGL library could possibly have been designed so that that code would look different, e.g.:
GLFWwindow mainWindow = glfwCreateWindow(WIDTH, HEIGHT, "Test Window", nullptr, nullptr);
or in fact, just using a construction of a GLFWindow
:
GLFWwindow mainWindow { WIDTH, HEIGHT, "Test Window", nullptr, nullptr };
still, there are several possible reasons why the API was designed this way:
- Until C++ 2017, compilers were not required to perform RVO - return value optimization. That meant that if your function returns a value of type
T
, you could theoretically need to copy the T
you create within the function to the destination location at the code which called the function. In your case - returning a GLFWwindow
might have required making a copy of the GLFWwindow
; and that could possibly have been problematic for the library (I don't know OpenGL so I have no idea).
- When your function returns a
T*
, the pointed-to object can be anything that "is a T" - which means any type that inherits from T
(!); while if you return a plain T
- you can't return inheriting types. So if there is a class hierarchy of GLFWwindow
s, all inheriting from GLFWwindow
as a base class - it can make sense to to return a pointer to that base class, for you to handle generically.
PS - Having five constructor parameters, and passing arguments which make it rather unclear what constructor parameter is being set, is problematic design. Specifically - we can't tell what the first nullptr
signifies, nor how it differs from the second nullptr
.