I recently used C++11's delegating constructors for the first time.
I thought that when I call a delegating constructor and I thow an
exception from the constructor that calls the delegating constructor
I've to call the destructor manually. But it's not like that:
#include <iostream>
using namespace std;
struct S
{
S() { cout << "S::S()" << endl; }
S( int ) : S() { throw invalid_argument( "sads" ); }
~S() { cout << "S::~S()" << endl; }
};
int main()
{
try
{
S s( 123 );
}
catch( ... )
{
}
}
Prints:
S::S()
S::~S()
I recently used C++11's delegating constructors for the first time.
I thought that when I call a delegating constructor and I thow an
exception from the constructor that calls the delegating constructor
I've to call the destructor manually. But it's not like that:
Um... Why would this even be a question? Why would the idea of "calling
the destructor manually" even enter consideration?
Am 01.10.2024 um 16:21 schrieb Andrey Tarasevich:
Um... Why would this even be a question? Why would the idea of "calling the
destructor manually" even enter consideration?
Because I've never used delegating constructors so far and I didn't
know where the object entered the state with complete construction.
If the object already "entered the state with complete construction",
then an explicit destructor results in UB when the destructor gets
called again, ...
Am 01.10.2024 um 16:21 schrieb Andrey Tarasevich:
Um... Why would this even be a question? Why would the idea of
"calling the destructor manually" even enter consideration?
Because I've never used delegating constructors so far and I didn't
know where the object entered the state with complete construction.
This is different from situation when regular constructor throws.
In that case individual sub-object destructors are invoked one by one.
Sysop: | DaiTengu |
---|---|
Location: | Appleton, WI |
Users: | 991 |
Nodes: | 10 (1 / 9) |
Uptime: | 75:29:29 |
Calls: | 12,948 |
Calls today: | 2 |
Files: | 186,574 |
Messages: | 3,264,524 |