explicit call to destructor is not destroying my object why?

前端 未结 7 534
我寻月下人不归
我寻月下人不归 2020-12-03 08:56

I\'m calling the destructor to deallocate memory but it is not deleting my object. What is the reason behind it?

my code is like this:

class A
{
publ         


        
7条回答
  •  囚心锁ツ
    2020-12-03 09:54

    Destructors were not designed to call them explicitly. Basically it is just another (special) method of class. If you want to Uninitialize your object and then still be able to use it you could make our own method:

    class B: public A
    {
    public:
     int b;
     B() {cout<<"b"<>x;
        return 0;
    }
    

    Otherwise B will be deleted when out of scope:

    int main()
    {
        {
          B b;
          b.b = 100;  //OK
        }
        b.b = 100; //compile time error
        int x;
        cin>>x;
        return 0;
    }
    

提交回复
热议问题