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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:32:03+00:00 2026-06-12T12:32:03+00:00

Possible Duplicate: Why does my destructor appear to be called more often than the

  • 0

Possible Duplicate:
Why does my destructor appear to be called more often than the constructor?

I have a static sCounter and int id to order my instances with id’s.

When grouping them in a vector, this works fine.. ID’s are setted corrrectly:

Task t("1st Task");
Task t2("2nd Task");
    Task t3("3rd Task");

std::vector<Task> myTasks;
myTasks.push_back(t);
myTasks.push_back(t2);
myTasks.push_back(t3);

task number one has ID 0, the second one: ID 1 and the third one ID 2.
However, if I do this:

std::vector<Task> myTasks;

myTasks.push_back(
   Task("First Task"));
myTasks.push_back(
   Task("Second Task"));
myTasks.push_back(
   Task("Third Task"));

The static sCount values starts giving strange results:

the first one gets ID 1, the second also ID 1, and the third gets ID 0.

Why does creating object like this mess with the static counter?

The sCounter gets increased on the constructor, and decreased on the destructor:

int Task::sCount = 0;

Task::Task(std::string text) {
    this->setText(text);
    this->setStatus(1);
    time_t now = time(0);
    timestamp = *localtime(&now);
    std::cout << "Count from " << sCount;
    sCount++;
    std::cout << " to --> " << sCount << "\n";
    this->setID(); // set the current sCount to ID
}
Task::~Task() {
    --sCount;
}

sCounter is a private static int: static int sCount;

  • 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-12T12:32:04+00:00Added an answer on June 12, 2026 at 12:32 pm

    In the second case, each Task instance passed to push_back is a temporary variable. This means that its destructor is called once the call to push_back is complete. The destructor then decrements the counter before the next Task is created.

    myTasks.push_back( Task("First Task") ); // Task's destructor is called before the next line
    

    Although the Task is copied into the vector (so the data exists elsewhere in memory), the implicit copy constructor that std::vector uses for this does not increment the counter.

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

Sidebar

Related Questions

Possible Duplicate: Does std::list::remove method call destructor of each removed element? Assume I have
Possible Duplicate: Does C# have extension properties? I have the following: public static class
Possible Duplicate: Does std::list::remove method call destructor of each removed element? I have a
Possible Duplicate: Does an open-ended interval implementation exist for Java? i have an int
Possible Duplicate: Does std::list::remove method call destructor of each removed element? I have a
Possible Duplicate: C++ Vector Pointers to Objects Does std::list::remove method call destructor of each
Possible Duplicate: Does fast enumeration in Objective-C guarantee the order of iteration? Just a
Possible Duplicate: Why does the background of TextView change it's size? I have a
Possible Duplicate: Does the order of class definition matter in PHP? class a {
Possible Duplicate: Does an abstact classes have a VTABLE? Does a vtable gets created

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.