Home > front end >  object destruction & delegating constructor
object destruction & delegating constructor

Time:03-03

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?

CodePudding user response:

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.

  • Related