If I have a class with two constructors like this:
class Test {
public:
Test(int x) {
_num = x;
}
Test() {
_num = 0;
}
private:
int _num;
};
I want to create a stack object based on a condition like this:
Test test;
if (someCondition() == 23) {
test = Test(42);
}
Will I have the overhead of creating a Test object two times, calling both constructors, in this case? Or will this be optimized out in general? Is this considered good practice?
Toy-examples in compiler explorer are optimized heavily with inlining with no apparent constructor call left. So it's not really clear to me.