Suppose I have a following class:
class Sample {
public:
Sample( int ) {}
};
some function returning an int
int SomeFunction()
{
return 0;
}
and this code:
Sample* sample = new Sample( SomeFunction() );
Now I expect the following sequence:
SomeFunction()
is run, then::operator new()
is run to allocate memory for the object, thenclass Sample
constructor is run over allocated memoryIs this order fixed or can it be changed by an implementation such that say first memory is allocated, then SomeFunction()
is called, then constructor is run? In other words, can call to operator new()
function and call to class constructor be interleaved with anything?
The order is unspecified. [5.3.4]/21 reads:
Whether [operator new] is called before evaluating the constructor arguments or after evaluating the constructor arguments but before entering the constructor is unspecified. It is also unspecified whether the arguments to a constructor are evaluated if [operator new] returns the null pointer or exits using an exception.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With