Calling The Constructor
Jan 31, 2011 at 9:05pm UTC
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 ;
Jan 31, 2011 at 9:15pm UTC
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.
Jan 31, 2011 at 9:31pm UTC
OK. Thanks for the reply, Disch.
Topic archived. No new replies allowed.