Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

object destruction & delegating constructor

According to this question about delegating constructors, a destructor is called when the first constructor has finished.
This is consistent with the following code:

struct test {
    test() { std::cout << "default constr\n"; }
    test(int) : test() { std::cout << "argument constr\n"; throw int{}; }
    ~test() { std::cout << "destr\n"; }
};

int main()
{
    try {
        test t{3};
    } catch(...)
    {
        std::cout << "caught\n";
    }
}

output:

default constr
argument constr
destr
caught

But, Stroustrup says the following in his book (4th edition, page 503):

An object is not considered constructed until its constructor completes (...). When using a delegating constructor, the object is not considered constructed until the delegating constructor completes – just completing the delegated-to constructor is not sufficient. A destructor will not be called for an object unless its original constructor completed.

Am I misreading this or does he mean something else?

like image 633
curiousguy12 Avatar asked Oct 11 '25 18:10

curiousguy12


1 Answers

Am I misreading this

I don't think so.

or does he mean something else?

I don't think so.

This seems to be an error in the book as far as I can tell. The book's description may be based on the original delegating constructors proposal. The behaviour was changed in following revisions of the proposal.

like image 93
eerorika Avatar answered Oct 14 '25 06:10

eerorika



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!