Is it possible to Overload a Destructor?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anupam
    New Member
    • Aug 2006
    • 12

    Is it possible to Overload a Destructor?

    Hi Banfa ,
    Can we overload destructor ? If yes then plz provide an example ?
    If not then why ?
  • dhirajrane
    New Member
    • Aug 2006
    • 1

    #2
    Originally posted by anupam
    Hi Banfa ,
    Can we overload destructor ? If yes then plz provide an example ?
    If not then why ?

    Answere

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    class abc
    {
    public:
    abc()
    {
    printf("Constructor is called\n");
    }
    ~abc()
    {
    printf("Destructor is called\n");
    }
    };
    class xyz:abc
    {
    public:
    xyz()
    {
    printf(" Derived Constructor is called\n");
    }
    ~xyz()
    {
    printf(" Derived Destructor is called\n");
    }
    };
    void main()
    {
    
    clrscr();
    abc *a = new abc();
    xyz *x = new xyz();
    //abc *b = new xyz();
    delete a;
    delete x;
    //delete b;
    getch();
    }
    :)
    Last edited by zmbd; Jan 1 '13, 10:10 PM. Reason: [Z{Placed <CODE/> tags in Zombie Resurection}]

    Comment

    • anupam
      New Member
      • Aug 2006
      • 12

      #3
      What was that dhirajrane ? I didn't mean the general constructor and destructor. It was overloaded destructor ( whether we can define more than one destructor in a class or not ) ?

      Comment

      • D_C
        Contributor
        • Jun 2006
        • 293

        #4
        Why would you need to delete something in more than one way? I don't see why it's necessary. Besides, you could always call a single destructor, and conditionally execute whichever destructor you wish.

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Normally (in fact almost always) you never explicitly call the destructor, it is automatically called by the C++ frame work when the object is deleted/goes out of scope. Therefore you would not need to overload the destructor because the frame work always calls the default destructor.

          Additionally the C++ standard defines that a conforming compiler only allows for 1 destructor per class.

          Comment

          • anupam
            New Member
            • Aug 2006
            • 12

            #6
            Thanks all of u.

            Comment

            • uday chandu
              New Member
              • Jan 2013
              • 1

              #7
              There is no reason for overloading (i mean two function with same name) a destructor in c++, because a destructor is called automatically at last and it will clean the allocated memory. so once allocated memory is de-allocated by one destructor what the need for another function, so only c++ doesn't support destructor overloading .

              Comment

              • sripal
                New Member
                • Nov 2014
                • 1

                #8
                Uday


                i have one Question here,
                i got one point we cant overload a destructor,
                can we have override destuctor?

                Comment

                • Banfa
                  Recognized Expert Expert
                  • Feb 2006
                  • 9067

                  #9
                  No, destructors (and constructors) are special and you can not over-ride a destructor, nor should you need to.

                  In a class heirarchy

                  Code:
                  class Base
                  {
                  public:
                      Base() {}
                      ~Base() {}
                  };
                  
                  class Derived : public Base
                  {
                  public:
                      Derived() {}
                      ~Derived() {}
                  };
                  When you construct an instance of Derived after the memory is allocated the first thing that happens is the Base() is called, the Derived() is called.

                  Destruction happens in a similar manor but reverses the order, first ~Derived() is called, then ~Base() is called.

                  You can not over-ride ~Base in Derived because the compiler knows that the method starting with ~ is the destructor and that the symbol name following should be the name of the class. There is also no need to over-ride as the Base destructor is going to be called anyway and should be doing the appropriate clean-up for Base.

                  Comment

                  Working...