I have a non-pointer class member that I need to initialize in the constructor:
class Alerter {
protected:
Timer timer;
public:
Alerter(int interval);
};
and then
Alerter::Alerter(int interval) {
timer = createTimer(interval);
}
(simplified code just to demonstrate the problem).
I have some doubts and concerns that timer
probably is first created using its parameter-less constructor and later that instance is overwritten by the contents that the createTimer
function returns.
How good is the approach? The possible answers could be:
- The "empty timer" created by its parameter-less constructor is not actually created, as the compiler is smart enough to find we have never referenced it before overwriting the value.
- The empty timer is created, but this is OK as well-written code should support a cheap parameter-less constructor for throwaway instances.
- It's better to use pointers.
Which of these assumptions (or maybe something else) is the most correct?