8

I'm wondering what is the best way to go about object initialization and storage with regards to objects that have to have a relatively large scope / long lifetime. Let's say we have a GameEngine class that needs to initialize and hold a reference to a Window for rendering. The reference is needed throughout the program's lifetime and the window needs to know its dimensions, at least.

In Java, I'd do it like this:

// Declaration:
Window window;
// Initialization:
window = new Window(width, height);

I understood that in C++, the first would already call the default constructor of the Window class, hence be declaration and initialization. Having a window = Window(width, height); would therefore be assignment, throwing away the already existing object.

The first solution I could find was to use a pointer:

// GameEngine.hpp
class GameEngine {
    Window *window;
};

// Somewhere in GameEngine.cpp:
window = new Window(width, height);

But then again, I constantly read one should favor plain objects over pointers whenever possible and in fact, I got myself into a mess of pointers in no time, so I am looking for another way.

Another solution seems to design your objects to have a constructor without parameters and set up the object later on:

// GameEngine.hpp
class GameEngine {
    Window window;
};

// Somewhere in GameEngine.cpp
window.setWidth(width);
window.setHeight(height);

This works, but has a serious drawback: the object (at least in this case) could be in an inconsistent state, as trying to display the window without setting width/height would result in an error or crash. It does work for some objects, but for most it does not.

One way to avoid this would be to have default values. For example, the constructor for the Window class could look like this:

Window::Window(int width = 800, int height = 600) {}

Or even like that:

Window::Window() : width(DEFAULT_WIDTH), height(DEFAULT_HEIGHT) {}

But in many cases, default values will be hard to determine. Also, where should they be coming from? Should the Window class define DEFAULT_WIDTH and DEFAULT_HEIGHT? Or should I even do this?

// GameEngine.hpp
class GameEngine {
    static const int DEFAULT_WIDTH = 800;
    static const int DEFAULT_HEIGHT = 600;
    Window window(800,600);
};

But that seems bad, as I've read that you should not do any initialization in the header, only declaration, so the values of DEFAULT_WIDTH and DEFAULT_HEIGHT should not actually be known at this point (and only be initialized in the .cpp, correct?).

Am I missing an option? Or is it common in C++ to assume that the programmer should know what he's doing and take care of getting his objects in a consistent state before using them? When to use which approach?

9
  • 1
    @close vote - this doesn't look like an opinion based question to me. While a bit poorly phrased (probably due to lack of understanding) the question is really about the technicalities of the C++ language.
    – Cubic
    Jun 3, 2015 at 22:08
  • I think it is asking about how to do two-phase initialization
    – M.M
    Jun 3, 2015 at 22:09
  • 1
    I feel like I'm missing something; is there a reason why you cannot create the Window object and initialize it at the same time? You need the declaration and the initialization to be in different places? Because if you don't, then just include the initialization in the declaration statement and there's no problem.
    – DrGoldfire
    Jun 3, 2015 at 22:12
  • @MattMcNabb Huh. I thought the question was about how to avoid doing two-phase initialization.
    – Cubic
    Jun 3, 2015 at 22:13
  • 1
    @domdom two-phase initialization means you initialize some things during construction and then initialize some things later. You're describing initialization and then assignment, which is one way of implementing two-phase; but not the only way. My answer shows another way which doesn't involve assignment. There's no single correct answer to this question , it's a design decision that is usually based on experience! So I would advise to just do something, and then you will learn the pros and cons of it by seeing how your project turns out.
    – M.M
    Jun 3, 2015 at 22:24

5 Answers 5

5

If you want to construct it only once and it can be done in the initialization of the class then you dont need a pointer. You can declare it as a member and initialize it in the constructor like so:

HPP

class Game
{
    private:
        Window window_;

    public:
        Game(int, int);
}

CPP

Game::Game(int width, int height) : window_(width, height)
{
}

This will construct the window object when you construct the Game object and it will persist until the Game object is destroyed. If you want to be able to construct it later or reconstruct it at any time then use a std::unique_ptr like so:

HPP

class Game
{
    private:
       std::unique_ptr<Window> window_;

    public:
        Game(int, int);
        void SomeMethod(int, int);

}

CPP

Game::Game(int width, int height)
{
    window_ = std::make_unique<Window>(width, height);
}

Game::SomeMethod(int width, int height)
{
    window_ = std::make_unique<Window>(width, height);
}

This will automatically delete the window when the Game object is destroyed and automatically delete the window each time you call std::make_unique to build a new one. Here is some doc on unique_ptr: http://en.cppreference.com/w/cpp/memory/unique_ptr

5
  • I think this actually made me realize my conceptual mistake: I assumed having class GameEngine { Window window; } would already call Window's ctor, because Window window; would do that if it would occur like that in some function, right? But the fact that it does not call the ctor here and I can initialize the object in GameEngine's member initialization list is indeed a satisfying solution for most cases. Thanks for helping with a beginner's confusion!
    – domsson
    Jun 3, 2015 at 22:29
  • 1
    Correct, declaring something as a member of a class does not construct it. It will be constructed when the class is constructed. Jun 3, 2015 at 22:33
  • One more question; what would happen if I would not initialize window in the ctor of Game? Would it be still uninitialized or would the compiler call the default ctor of Window?
    – domsson
    Jun 3, 2015 at 22:52
  • 1
    If a constructor is not explicitly called for a member in the initialization of a class then it will try to use a default no parameter constructor for it, if non exists then you will get a compile error. So if window had a no parameter constructor then that will be called otherwise it wont compile Jun 3, 2015 at 22:56
  • Ah, got it. You helped me a lot, Josh. Thank you!
    – domsson
    Jun 3, 2015 at 23:09
2

You're apparently misunderstanding C++. You'd never have Window window; just like that in a header. That defines a Window object, every time the header is included !

You may have class GameEngine { Window window; .... } but that doesn't actually crate a window at all. Each GameEngine constructor has an initializer list, and there you do initialize window. Makes sense: the game engine creates the window it needs.

1
  • Sorry, I obviously tried to keep the code excerpts too short. That Window window; would be inside the class definition, of course. I will edit accordingly. This would then be correct though, right?
    – domsson
    Jun 3, 2015 at 22:14
1

If you're talking about class members, then declaration is not the same point the constructor is being called. Initialization of such members is exactly what initializer lists (which you do seem to know about) are for!

class Window {
   int x;
   int y;
 public:
   Window(int x, int y);
 };

and

class Game {
 Window window;
public:
 Game();
};

Then you can call the constructor of the window class from the game constructor like so:

Game::Game() : window(DEFAULT_HEIGHT, DEFAULT_WIDTH) {}

In case you were talking about globals: If you really need a global object (although you probably don't want that) you can (and should!) declare the object with external linkage in the header (which will only make the name available, but not call any constructors) and do the definition in the implementation:

Declaration:

extern Window window;

Implementation:

Window window(DEFAULT_WIDTH, DEFAULT_HEIGHT);
1
  • Thank you. Your first sentence pretty much summarized what got me confused; I thought class member declaration would trigger their constructors. If I could, I'd mark your answer accepted as well, but I had to choose one. :\
    – domsson
    Jun 3, 2015 at 22:36
1

Ideally, you would design your classes so that all the initialization you need can occur in the constructor. Example here.

But this isn't always possible (e.g. if you want a Window that isn't created until some particular event happens during the Game); or it can be hard to wrap your head around as a new programmer.


One approach is to use pointers - but use a smart pointer instead of a raw pointer.

If your class needs to contain some object handles but you aren't ready to create the object yet, then you can have a class member:

std::unique_ptr<Window> p_window;

Then when you are ready to create the window, you can execute the code:

p_window.reset( new Window(bla bla bla) );

The smart pointer takes care of calling delete when its containing object is destroyed, and it will give a compile error if you accidentally try to do a "shallow copy".

To use the pointer once it is pointing somewhere you would write p_window->bla..., and to check if it has been assigned yet you can use if ( p_window ).

1
  • Thank you, too. I think I now understand what my misconception was. Sorry for my beginner's confusion. Again, thanks for the input. :)
    – domsson
    Jun 3, 2015 at 22:38
0

The option you're missing is to initialize Window objects when they're created. Don't declare Window objects in your functions before you know how to initialize them. If you have an object with a Window members, have the constructor to the object initialize the Window member.

Pointers are fine and the right thing to do if the time of creation of an object really is indeterminate, or you really and truly need to declare a variable for it before you are ready to create a valid object.

The point of the advice you mention is not to change how you design objects, but that you need to reconsider how you use the objects: you need to unlearn habits you've picked up from everything-is-a-pointer programming environments like Java.

(although you should use smart pointers, like unique_ptr or shared_ptr as appropriate)

(also, if you believe a class will pretty much always need to be used with pointers, it is useful to make a wrapper class around the pointer that acts like a "plain object" even though its implemented with a pointer inside)

1
  • Yes, I think coming from Java can really lead to some confusion when trying to wrap one's head around C++. I'm struggling more than I thought I would. It is slowly coming together though. Thanks for your input. :)
    – domsson
    Jun 3, 2015 at 22:41

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.