Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8502725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:33:55+00:00 2026-06-11T01:33:55+00:00

I was playing with C++ class constructor function by using it recursively to print

  • 0

I was playing with C++ class constructor function by using it recursively to print “trauth table”. Everything was seem to be normal until I decided “why not using destructor too recursively?”. When I implemented printing the “trauth table” with destructor also, I noticed that the destructor is being called every time the control returns from a recurisve call to constructor.

OUTPUT SNIPPET

Calling hello_world class constructor...
000
A destructor call within initialization has been terminated
001
A destructor call within initialization has been terminated
A destructor call within initialization has been terminated
010
...
...

THE CLASS

#define MAX 3
class hello_world
{
    char *const buf;
    int stack_ptr, destructor_calls;
    bool init;
public:

    // The recursive constructor

    hello_world(char *const &str, int i = 0)
        : buf(str), stack_ptr(0), init(false), destructor_calls(0)
    {
        if (i == MAX)
        {
            buf[i] = '\0';
            cout << buf << endl;
        }
        else
        {
            buf[i] = '0';
            hello_world::hello_world(str, i + 1);
            buf[i] = '1';
            hello_world::hello_world(str, i + 1);
        }
    }

    // The recusive destructor

    ~hello_world()
    {
        ++destructor_calls;
        if (!init) { cerr << "A destructor call within initialization has been terminated" << endl; return; }

        int i = stack_ptr;
        if (i == MAX)
        {
            buf[i] = '\0';
            cout << buf << endl;
        }
        else
        {
            buf[i] = '0';
            ++stack_ptr; // since a destructor cannot take parameters
            hello_world::~hello_world();
            --stack_ptr;
            buf[i] = '1';
            ++stack_ptr;
            hello_world::~hello_world();
            --stack_ptr;

            // Printing total number of calls at final call
            if (i == 0) cout << endl << "\"destrucotr_calls\" = " <<
 destructor_calls << endl;
        }
    }

    void unlock()
    {
        init = true;
    }
}; // end of class hello_world

I’m using an int hello_world::stack_ptr to store current number of i parameter, since a destructor cannot have parameters.

My question is: Why the destructor is being called each time the control leaves a recursive call to the constructor?.

Here is my main():

void main()
{
    char buf[MAX + 1];
    cout << "Calling hello_world class constructor..." << endl;
    hello_world h(buf);
    h.unlock();
    cout << "Calling hello_world class destructor..." << endl;
}

I’m using VS2010. You can view the complete code along with its output here.

ADD: I’m attempting to count total number of calling destructor with int hello_world::destructor_calls. I have found that printing trauth table algorithm takes 2 * (2^MAX) - 1 calls and, at the end, destructor_calls exactly equals this value. However, when counting the sentence "A destructor call within initialization has been terminated" in output, we find that it has been output 14 times. So 14 (calls within initialization) + 15 (natural calls of printing trauth table) should equal 29 while destructor_callsequals 15 only (as if the destructor has’t been called at initialization !!)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T01:33:57+00:00Added an answer on June 11, 2026 at 1:33 am

    There’s no such thing as a recursive constructor. What looks like a recursion is actually creating a new temporary object.

    It is possible to call the destructor directly, but if you call a destructor twice on the same object, you get undefined behavior, so you can’t call it recursively.

    What you will need to do is call another member function within the constructor or destructor. You can then make these other functions be recursive:

    class A {
      A()  { construct(); }
      ~A() { destruct(); }
      void construct(int i=0) { ... construct(i+1); ... }
      void destruct(int i=MAX) { ... destruct(i-1); ... }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm playing with DI (using Unity). I've learned how to do Constructor and Property
I'm using the Boost Parameter tutorial to create a named-parameter constructor for a playing
I was playing with JavaScript class inheritance (I am using node.js). I get undefined
I am playing about with Mootools class inheritance etc and I am trying to
Was playing around a little bit with the TimeSpan class and I started wondering
In AS3 I create a number of playing cards through a class that extends
I'm playing with Hibernate a little bit. I created a Group class and a
I am currently playing around with VB.Net using Visual Studio 2010 (.Net 4.0) and
I was playing with some code to make a "closure like" construct ( not
I'm using static ArrayList in a class to store information about non-updatable database fields.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.