Delaying class member initialization

Lately I've been working on an OpenGL project as a hobby. This project contains a Window class that wraps all window callbacks used by the GLFW library, which I use for creating windows.

Currently the class looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
class Window
{
    private:
        //... some members here
        GLFWwindow* window;
        Camera camera;
        //... more members follow
    public:
        Window(GLFWwindow* window);
        ~Window();
        //... other public members follow
};


As you can see here, the constructor requires a single argument, a pointer to a GLFWwindow data structure. The class also has a member of type Camera, which represents the game camera and processes all view transformations. The camera class looks like the following definition:

1
2
3
4
5
6
7
8
class Camera
{
     private:
         // ... A lot of private members
    public:
        Camera(int width, int height);
        ~Camera();
};


The camera class has a constructor taking the screen width and height as its parameters, and no default constructors. To construct a camera in the Window class, I currently use this approach:

 
Window::Window(glfwWindow* window) : window(window), camera(600, 400) {}


This uses preset values for the window width and height. This of course isn't all that useful, since this would mean the class could only be used on fixed-size windows.

To retrieve the sizes required by the camera, I could use the following code:

1
2
3
4
5
6
Window::Window(glfwWindow* window) : window(window)
{
    int window_width, window_height;
    glfwGetFramebufferSize(this->window, &window_width, &window_height);
    this->camera = Camera(window_width, window_height);
}


Now the problem is that by the time the code reaches the constructor the initializer list is completed and a default constructor for the Camera class should have been executed, but since the Camera class has no default constructor, this simply won't compile. Is there any way to delay the initialization of the Camera class so I can construct it within the constructor? Preferably without defining a dummy default constructor or allocating the camera on the heap.
maybe you can define a constructor for Camera that accepts a glfwWindow*:

1
2
3
4
Camera::Camera(GLFWwindow* window)
{
	glfwGetFramebufferSize(window, &width, &height); // width and height are member variables of Camera
}

and then use it in Window constructor:
1
2
3
Window::Window(GLFWwindow* window) : window(window), camera(window)
{
}

Topic archived. No new replies allowed.