Repeat Destructor vs member

I already saw a similar question: The destructor against the race of member functions .. but could not find the answer to the following. Suppose we have a class that owns some workflow. A class destructor might look like this:

~OurClass
{
    ask_the_thread_to_terminate;
    wait_for_the_thread_to_terminate;
    ....
    do_other_things;
}

The question arises: can we name ourClass member functions in the workflow, because we are sure that all these calls will be made before do_other_things in the destructor?

+4
source share
3 answers

Yes, you can. Destruction of member variables will only begin after completion do_other_things. It is safe to call member functions before the destruction of the object.

+4

, , . , - :

~OurClass
{
    delete *memberPointer_; 
    ask_the_thread_to_terminate; // the thread calls OurClass::foo();
    wait_for_the_thread_to_terminate;
    ....
    do_other_things
}

void OurClass::foo()
{
    memberPointer->doSomething();
}

. . . , , , .

+2

, - , . , (, SingerOfTheFall).

+1

Source: https://habr.com/ru/post/1537677/


All Articles