Virtual function call in derived class constructor & destructor

question 20 Reputation points
2023-11-27T08:27:39.5+00:00

https://learn.microsoft.com/cpp/cppcx/ref-classes-and-structs-c-cx?view=msvc-170

I saw the following in this link.

The behavior of trying to access a member of a class that has already allowed the destructor to run is not defined. There is a high possibility that the operation of the program will be interrupted.

I want to call a virtual function that handles a member within a derivative class constructor, a destructor. Even if I pay attention to the initialization order (by initializing members after the virtual function call is complete), is there a possibility that an undefined action will occur because the destructor has already been executed? Or is it okay to say that it's safe if I just pay attention to the order?

Developer technologies C++
{count} votes

Accepted answer
  1. Minxin Yu 13,501 Reputation points Microsoft External Staff
    2023-11-28T01:58:28.9633333+00:00

    Hi, @question

    delete Arr; It is recommended to use delete[] Arr;

    log(); //In experiment1 and //log(); //In experiment4

    The variable has not been assigned a value or has been deleted.

    Reading an uninitialized value results in undefined behavior.

    Best regards,

    Minxin Yu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.