0

Here I have called destructor explicitly , but it is not destroying object. In show function I have destroyed the object but still I am able to access show2() ,How ?


class Test{
public:
    Test() {
        cout<<"constructor\n";
    }
    Test(const Test&t) {
        cout<<"Copy constructor\n";
    }
    ~Test() {
        cout<<"Destructor\n";
    }
    void show() {
        cout<<"Show Begin\n";
        this->~Test();
        cout<<"Show End\n";
    }

    void show2() {
        cout<<"Show2 Begin\n";
        this->Test::~Test();
        cout<<"Show2 End\n";
    }

};

int main() {
    Test t;
    t.show();
    t.show2();
    return 0;
}

Output

constructor
Show Begin
Destructor
Show End
Show2 Begin
Destructor
Show2 End
Destructor
  • 4
    Explicit destructor calls are almost never the answer. – Silvio Mayolo May 11 '21 at 02:43
  • a destructor is like a callback function when the object gets deleted, it is not the method from which it frees the memory, so when you call the ~Test() method its like calling any other function – VictorJimenez99 May 11 '21 at 02:44
  • 1
    @VictorJimenez99 that is incorrect and misleading. A call to the destructor _ends the lifetime of the object_. It is very different from calling normal methods and should almost never be done in everyday C++ code. – alter_igel May 11 '21 at 02:47
  • A destructor is a member function that is invoked automatically when the object goes out of scope or is explicitly destroyed by a call to delete [microsfot, https://learn.microsoft.com/en-us/cpp/cpp/destructors-cpp?view=msvc-160] im not saying its correct, im saying that it can be called. – VictorJimenez99 May 11 '21 at 02:51
  • @VictorJimenez99 - There is only one condition where you should *ever* call the destructor explicitly, and this isn't it. – Stephen Newell May 11 '21 at 03:05
  • 2
    A destructor is a member function that is invoked automatically, mind the "invoked automatically" . so the sequence is obj goes out of scope -> call destructor for cleanup, not the other way around. you can do the same with the constructor https://onlinegdb.com/H1SDEuwuu . but the entire thing is more like changing the fuel tank during a flight , if you are doing it you are doing something wrong – saumitra mallick May 11 '21 at 03:09
  • @StephenNewell again, I´m not saying its correct. OP was asking why you can call methods of an object after calling its destructor. I answered because a call to its destructor doesn´t destroy the object. thats it. – VictorJimenez99 May 11 '21 at 03:10
  • First, you are not destroying the object. You are only invoking its destructor. The object destruction happens in this case when the object falls out of scope. Second, if these were virtual methods that overrode virtual methods in a base class, you would indeed see a change in behaviour: you would see the base class methods being called. But your code isn't valid, as it will result in the destructor being invoked more than once. – user207421 May 11 '21 at 03:24
  • This question is reminiscent of [Can a local variable's memory be accessed outside its scope?](https://stackoverflow.com/questions/6441218) In both cases, the question presents code that invokes [undefined behavior](https://stackoverflow.com/questions/2397984), then asks why a certain result is possible. The answer is the same: undefined behavior means **anything** can happen, including acting as if nothing special happened. – JaMiT May 11 '21 at 04:13
  • you can find your answer here [Is it possible to call constructor and destructor explicitly?](https://www.geeksforgeeks.org/possible-call-constructor-destructor-explicitly/) – Pouya Imani May 11 '21 at 04:21
  • The answers to [Explicit call to destructor](https://stackoverflow.com/questions/53262269/explicit-call-to-destructor) might be a useful read. – JaMiT May 11 '21 at 04:22
  • @user207421 Invoking the destructor ends the lifetime of the object: **[class.dtor]** "Once a destructor is invoked for an object, [the object's lifetime ends](https://timsong-cpp.github.io/cppwp/class.dtor#19)". No further method calls on the object are allowed. In the above example, there are two violations of this rule: Calling `show2()` on a destroyed object, and then destroying an already-destroyed object (when `t` goes out of scope). If you violate the rule, then the program is in error and anything can happen. – Raymond Chen May 11 '21 at 04:24

0 Answers0