Well, if Programmer_P ever uses Sebastiani's idea, then comes round asking why there was a mismatch in the number of constructor and destructor invocations, then Sebastiani better be at hand to give a good answer, or we get to roast him over a slow burning fire
Fair enough, but let's face it - given his track record, the chances that he actually follows any advice here is pretty slim.
No, it does not. We're dealing with a new object. What happens is that the constructor is invoked to create a new object, which is then placed in memory in the same location as an existing object.
Actually, the constructor is invoked on the existing object, just as if it had been declared at the point of the "placement new" call.
Code:
template < typename Type >
struct reference
{
reference( Type& data )
: data( data )
{
cout << "Construct: " << static_cast< void* >( this ) << endl;
}
Type&
data;
};
int main( void )
{
int
i = 1024,
j = i + 1;
reference< int >
r( i );
cout << r.data << endl;
new( &r ) reference< int >( j );
cout << r.data << endl;
}
Output:
Construct: 0x28ff10
1024
Construct: 0x28ff10
1025
Two constructor calls, one address.