Re: Order of destructor execution.
On Jul 23, 6:03 pm, eshn...@gmail.com wrote:
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 ):
Just curious as to what you thing the volatile is going to do
here. (Under Posix, at least, and supposing that your
MutexWrapper actually wraps a pthread_mutex_t, all it does is
slow up your code, without providing any additional guarantees.)
unsigned getCont( void )
{
MutexLocker( mut );
If the above compiles, it's time to change compilers.
MutexLocker doesn't have a default constructor, so you can't
declare an instance of it without providing an argument.
return( cont );
}
Will the destructor ~MutexLocker() be executed before the copy
constructor of unsigned?
Since the code won't compile, it's hard to say anything about
its runtime behavior. Local variables however, will only be
destructed after the return value has been copied to where ever
it is to be copied to.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34