Calling The Constructor

closed account (zb0S216C)
I was messing around with my variable which is of type bool. I tried calling the variable's constructor after I had already defined it, like this:
1
2
3
4
bool MyVar( false );

// Re-construct MyVar. This is valid in GCC.
MyVar = bool( true );


Is this the same as this?:
1
2
bool MyVar( false );
MyVar = true;
For basic types, yes.

For complex types (classes), not quite. It's actually more like this:

1
2
3
4
5
bool MyVar(false);
{
  bool temporary(true);
  MyVar = temporary;
}


With basic types, the compiler can optimize and remove 'temporary', but with complex types (like string, vector, or really any class), it cannot remove the temporary object as that would break language rules.
closed account (zb0S216C)
OK. Thanks for the reply, Disch.
Topic archived. No new replies allowed.