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

  • SEARCH
  • Home
  • 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 6900399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:32:29+00:00 2026-05-27T07:32:29+00:00

Given this code: #include <iostream> using namespace std; class Foo { public: Foo ()

  • 0

Given this code:

#include <iostream>
using namespace std;

class Foo {
public:
    Foo ()          {   c = 'a'; cout << "Foo()" << endl;       }
    Foo (char ch)   {   c = ch; cout << "Foo(char)" << endl;    }
    ~Foo ()         {   cout << "~Foo()" << endl;               }

private:
    char c;
};

class Bar : public Foo {
public:
    Bar ()                      {   cout << "Bar()" << endl;    }
    Bar (char ch) : Foo(ch)     {   cout << "Bar(char)" << endl;    }
    ~Bar ()                     {   cout << "~Bar()" << endl;           }
};

Foo f1; static Bar b1;

int main()
{
    Bar b2;

    {
        static Foo f2('c');
        Foo f3;
        Bar b3 ('d');
    }

    return 0;
}

(You can just paste this directly into a compiler)

The first part of my expected sample output is correct:

Foo()
Foo() 
Bar() 
Foo()
Bar()
Foo(char) 
Foo()
Foo(char)
Bar(char)
~Bar()
~Foo
~Foo()
~Bar()
~Foo()
~Foo()

But I get the destructor output of the two static objects static Bar b1; and static Foo f2('c'); wrong.

The correct answer for the last part is:

~Bar()
~Foo()
~Foo()

I get:

~Foo()
~Bar()
~Foo()

This is my reasoning:

I understand that all local objects are destructed before static objects. Of the two remaining static objects static Bar b1; and static Foo f2('c');, static Foo f2('c'); appears last, so it is destructed first, because destructors are called in the reverse order of their creation.

But static Foo f2('c'); isn’t destructed first, static Bar b1; is. Why?

  • 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-05-27T07:32:29+00:00Added an answer on May 27, 2026 at 7:32 am

    Modified you program :

    #include <iostream>
    using namespace std;
    
    class Foo {
    public:
        Foo ()          {   c = 'a'; cout << "Foo()" << endl;       }
        Foo (char ch)   {   c = ch; cout << "Foo(char)" << ch << endl;    }
        ~Foo ()         {   cout << "~Foo()"<< c << endl;               }
    
    protected:
        char c;
    };
    
    class Bar : public Foo {
    public:
        Bar ()                      {   cout << "Bar()" << endl;    }
        Bar (char ch) : Foo(ch)     {   cout << "Bar(char)" << ch << endl;    }
        ~Bar ()                     {   cout << "~Bar()" << c << endl;           }
    };
    
    Foo f1('a'); static Bar b1('b');
    
    int main()
    {
        Bar b2('c');
    
        {
            static Foo f2('d');
            Foo f3('e');
            Bar b3 ('f');
        }
    
        return 0;
    }
    

    Which generates the following output in g++ 4.5.2:

    Foo(char)a
    Foo(char)b
    Bar(char)b
    Foo(char)c
    Bar(char)c
    Foo(char)d
    Foo(char)e
    Foo(char)f
    Bar(char)f
    ~Bar()f
    ~Foo()f
    ~Foo()e
    ~Bar()c
    ~Foo()c
    ~Foo()d
    ~Bar()b
    ~Foo()b
    ~Foo()a
    

    You see that the last destructed one is the non-static global variable Foo f1.

    EDIT:
    As the others mentioned, the initialization order of variables with static storage duration is unspecific if the variables are from different translation units, but they can be defined when they are in the same translation unit.

    Initialization by constructor calls (as in this examples) are called dynamic initialization, and

    Dynamic initialization of a non-local variable with static storage
    duration is either ordered or unordered
    . Definitions of explicitly
    specialized class template static data members have ordered
    initialization. Other class template static data members (i.e.,
    implicitly or explicitly instantiated specializations) have unordered
    initialization. Other non-local variables with static storage duration
    have ordered initialization. Variables with ordered initialization
    defined within a single translation unit shall be initialized in the
    order of their definitions in the translation unit.

    It is implementation-defined whether the dynamic initialization of a
    non-local variable with static storage duration is done before the
    first statement of main. If the initialization is deferred to some
    point in time after the first statement of main, it shall occur before
    the first odr-use (3.2) of any function or variable defined in the
    same translation unit as the variable to be initialized.

    The initialization of local static variables is specified as

    … such a variable
    is initialized the first time control passes through its declaration; …

    And as the destruction of variables with static storage duration should be in the reverse order of their construction, so the order of construction and destruction of the variables with types Foo and Bar in this example is in fact defined.

    Again, when you have multiple translation, you’d better not to rely on the order of initialization.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given this sample code: #include <iostream> #include <stdexcept> class my_exception_t : std::exception { public:
Consider the following sample code: #include <iostream> using namespace std; class base { public:
Here is my code - #include<iostream> using namespace std; class base { public: void
How this code modifies a value in a const function: #include<iostream> using namespace std;
I have this code main.cpp #include <iostream> #include functs.h using namespace std; int main()
I have the following code: #include <iostream> using namespace std; class testing{ int test()
#include <iostream> #include <fstream> using namespace std; class Integer { public: int i; Integer
#include <iostream> #include <set> using namespace std; class StudentT { public: int id; string
Conside the following sample code below: #include <iostream> using namespace std; class base {
Given this code: #include <iostream> #include <vector> #include <fstream> #include <sstream> #include <boost/regex.hpp> using

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.