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 9135061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:46:33+00:00 2026-06-17T08:46:33+00:00

Possible Duplicate: Object destruction in C++ Suppose we have two classes, one is called

  • 0

Possible Duplicate:
Object destruction in C++

Suppose we have two classes, one is called Array and the other one is called MessageOnExit.

Assume the Array class has a static data member called counter.

Here are the classes:

class Array {
public:
    static int counter;
    Array(){counter++;}
    ~Array(){counter--;}
};

class MessageOnExit {
public:
    ~MessageOnExit(){cout << Array::counter;}
};

The following is the “main” using these classes:

// static variable definition
int Array::counter;
// global object definition
MessageOnExit message;

void main() {
    Array a1, a2;
    Array a3(a1);
}

The first two declarations will change counter to be 2 ensues two calls of the Empty/Default constructor
The third declaration won’t change the counter because the Default copy constructor will be called.

Now when the main execution is done, the value of the static member counterwill be -1 ensues the destructors calls (after a1, a2 and a3 are desroyed), and then the message destructor will be called, printing this value (-1).

My question is how do we know the static member counter is still alive and available when the message destructor is being called.

Also if I change the static/global object/variable definition order, that is, message is defined before counter, the value remains -1.

To summarize, no matter what changes I do, when the program terminates the message destructor success to access the static member counter and I don’t understand why is that. As far as I understand if the global object message is defined first its destructor will be called last and therefore the counter value won’t be available for its destructor.

Thanks in advance!
Guy

  • 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-17T08:46:34+00:00Added an answer on June 17, 2026 at 8:46 am

    Standard section 3.8 says:

    The lifetime of an object of type T ends when:

    • if T is a class type with a non-trivial destructor (12.4), the destructor call starts, or

    • the storage which the object occupies is reused or released.

    Now int is not a class type, and since Array::counter has static storage duration, its storage is never released. So the lifetime of Array::counter never ends, and you can safely access it from any destructor you want!

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

Sidebar

Related Questions

Possible Duplicate: Why do this Ruby object have two to_s and inspect methods that
Possible Duplicate: Converting any object to a byte array in java I have a
Possible Duplicate: convert object to array Let's say I have an array like this:
Possible Duplicate: Intercept object on method invocation with Mockito I have an class that
Possible Duplicate: Find object by id in array of javascript objects So I have
Possible Duplicate: Why is the destructor not called for the returned object from the
Possible Duplicate: Comparing object properties in c# Let's say I have a POCO: public
Possible Duplicate: Does std::list::remove method call destructor of each removed element? Assume I have
Possible Duplicate: JSON object to listView i have a json data on server and
Possible Duplicate: Dynamic object property name I have an object like this var localAppConfig

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.