Order of destructor execution.
Sorry if this may seem to be very obvious, but it is important and I
decided to ask.
I have the following classes:
class MutexWrapper
{
public:
MutexWrapper( void );
void lock( void ); //Maybe const, whatever.
void unlock( void ); //As well.
~MutexWrapper( void );
}
class MutexLocker
{
public:
MutexLocker( MutexWrapper& mut ){ mut.lock(); } //Perhaps it
could be const MutexWrapper& mut (?)
~MutexLocker( void ){ mut.unlock(); }
}
So that I want to sync acces to the variable
volatile unsigned cont
in the following piece of code (supposing there is a global
MutexWrapper mut ):
unsigned getCont( void )
{
MutexLocker( mut );
return( cont );
}
Will the destructor ~MutexLocker() be executed before the copy
constructor of unsigned? It seems logical if this return statement
could be interpreted as a placement new in the adress given by the
return variable, something like the equivalence of:
c = getCont();
and
{
MutexLocker( mut );
new( &c ) unsigned( cont );
}
If so, my code seems to be correct. Is it true?
Thanks,
Elias.