How to best handle C++ object initialization: empty constructors or pointers? -
i'm wondering best way go object initialization , storage regards objects have have relatively large scope / long lifetime. let's have gameengine class needs initialize , hold reference window rendering. reference needed throughout program's lifetime , window needs know dimensions, @ least.
in java, i'd this:
// declaration: window window; // initialization: window = new window(width, height); i understood in c++, first call default constructor of window class, hence declaration , initialization. having window = window(width, height); therefore assignment, throwing away existing object.
the first solution find use pointer:
// gameengine.hpp class gameengine { window *window; }; // somewhere in gameengine.cpp: window = new window(width, height); but again, read 1 should favor plain objects on pointers whenever possible , in fact, got myself mess of pointers in no time, looking way.
another solution seems design objects have constructor without parameters , set object later on:
// gameengine.hpp class gameengine { window window; }; // somewhere in gameengine.cpp window.setwidth(width); window.setheight(height); this works, has serious drawback: object (at least in case) in inconsistent state, trying display window without setting width/height result in error or crash. work objects, not.
one way avoid have default values. example, constructor window class this:
window::window(int width = 800, int height = 600) {} or that:
window::window() : width(default_width), height(default_height) {} but in many cases, default values hard determine. also, should coming from? should window class define default_width , default_height? or should this?
// gameengine.hpp class gameengine { static const int default_width = 800; static const int default_height = 600; window window(800,600); }; but seems bad, i've read should not initialization in header, declaration, values of default_width , default_height should not known @ point (and initialized in .cpp, correct?).
am missing option? or common in c++ assume programmer should know he's doing , take care of getting objects in consistent state before using them? when use approach?
if want construct once , can done in initialization of class dont need pointer. can declare member , initialize in constructor so:
hpp
class game { private: window window_; public: game(int, int); } cpp
game::game(int width, int height) : window_(width, height) { } this construct window object when construct game object , persist until game object destroyed. if want able construct later or reconstruct @ time use std::unique_ptr 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 automatically delete window when game object destroyed , automatically delete window each time call std::make_unique build new one. here doc on unique_ptr: http://en.cppreference.com/w/cpp/memory/unique_ptr
Comments
Post a Comment