Practical application of class destructor

后端 未结 6 1554
孤街浪徒
孤街浪徒 2021-01-06 09:48

I\'m currently trying to learn about classes and constructors/destructors. I understand what the two do, but I\'m having a harder time with the destructors because I can\'t

6条回答
  •  春和景丽
    2021-01-06 09:57

    Destructors are special member functions used to release any resources allocated by the object.

    The most common example is when the constructor of the class uses new, and the destructor uses delete to deallocate the memory.

    class Myclass
    {
        int *m_ptr;
        public:
            Myclass():m_ptr(new int)
            {
            }
            ~Myclass()
            {
                 delete m_ptr;
            }
            //ToDo: Follow Rule of Three
            //Provide definitions for copy constructor & copy assignment operator as well
    
    };
    

提交回复
热议问题