Thread: Can you call a constructor within a destructor, or vise vesa?

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    Can you call a constructor within a destructor, or vise vesa?

    folks, I came across questions about calling constructor or destructor.
    I am curious whether you can:

    Code:
    1. call a constructor in a constructor
    2. call a destructor in a constructor
    3. call a constructor in a destructor
    4. call a destructor in a destructor
    5. call a constructor in a member function
    6. call a destructor in a member function
    So I wrote the following code and want to share with you what I got. Comments are welcomed! Please check if I miss anything.

    1. call a constructor in a constructor
    Code:
    class A{
    public: 
        A(){
            A();
            x = 100;
            cout<<"A() "<<x<<endl;
        }
        int x;
        ~A(){
            x = 0;
            cout<<"~A() "<<x<<endl;
        }
    };
    int main()
    {
        A obj;
        return 0; 
    }
    Basically you are not actually calling the constructor, you are just generating a temp object.
    If you use this->A() to call then you will get an error.
    Code:
    error: calling type `A' like a method
    Also this brings a recursive calling. Soon you will use up memory and crash.

    2. call a destructor in a constructor
    Code:
    class A{
    public: 
        A(){
            this->~A();
            x = 100;
            cout<<"A() "<<x<<endl;
        }
        int x;
        ~A(){
            x = 0;
            cout<<"~A() "<<x<<endl;
        }
    };
    
    int main()
    {
        A obj;
        return 0; 
    }
    You can't call ~A() without qualifying: this->~A(). At least I didn't figure out a way to avoid it.
    Yes, you can call destructor in the class, and you can see it destructs the member variable x.

    3. call a constructor in a destructor

    Code:
    class A{
    public: 
        A(){
            x = 100;
            cout<<"A() "<<x<<endl;
        }
        int x;
        ~A(){
            A();
            x = 0;
            cout<<"~A() "<<x<<endl;
        }
    };
    
    int main()
    {
        A obj;
        return 0; 
    }
    Again, you are not calling constructor, just a temp obj.

    4. call a destructor in a destructor

    Code:
    class A{
    public: 
        A(){
            x = 100;
            cout<<"A() "<<x<<endl;
        }
        int x;
    
        ~A(){
            this->~A();
            x = 0;
            cout<<"~A() "<<x<<endl;
        }
    };
    
    int main()
    {
        A obj;
        return 0; 
    }
    This also gives you a dead loop.

    5. call a constructor in a member function

    Code:
    class A{
    public: 
        A(){
            x = 100;
            cout<<"A() "<<x<<endl;
        }
        int x;
        void foo()
        {
            cout<<"BEFORE foo"<<x<<endl;
            A();
            cout<<"AFTER foo"<<x<<endl;
        }
        ~A(){
            x = 0;
            cout<<"~A() "<<x<<endl;
        }
    };
    
    int main()
    {
        A obj;
        obj.foo();
        return 0; 
    }
    It works. just a temp obj.

    6. call a destructor in a member function

    Code:
    class A{
    public: 
        A(){
            x = 100;
            cout<<"A() "<<x<<endl;
        }
        int x;
        void foo()
        {
            cout<<"BEFORE foo"<<x<<endl;
            A();
            cout<<"AFTER foo"<<x<<endl;
        }
        ~A(){
            x = 0;
            cout<<"~A() "<<x<<endl;
        }
    };
    
    int main()
    {
        A obj;
        obj.foo();
        return 0; 
    }
    It works.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Your analysis seems basically correct. The only one that you would ever consider using is calling the destructor in a member function, but you would only use that in rare occasions when you are managing memory yourself and are freeing the memory used by the object elsewhere.

    Otherwise doing these things is impossible, useless, and/or dangerous. The constructor and destructor are generally called automatically, if you want to do it explicitly there are almost always better solutions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error C2664 - Trying to call an external Dll
    By jamez05 in forum C++ Programming
    Replies: 3
    Last Post: 08-08-2006, 06:07 AM
  2. Assembly example
    By Lynux-Penguin in forum C Programming
    Replies: 6
    Last Post: 04-24-2002, 07:45 PM
  3. Help needed with external call, WinExec() and timer.
    By algorithm in forum Windows Programming
    Replies: 9
    Last Post: 11-07-2001, 09:35 AM
  4. Pls help me to do this project in C I need source code
    By sureshmenon74 in forum C Programming
    Replies: 4
    Last Post: 10-04-2001, 06:57 AM
  5. call by reference and a call by value
    By IceCold in forum C Programming
    Replies: 4
    Last Post: 09-08-2001, 05:06 PM