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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:38:44+00:00 2026-05-10T22:38:44+00:00

I wrote a program out, which was all in one file, and the methods

  • 0

I wrote a program out, which was all in one file, and the methods were forward declared in a header. The program initially worked perfectly when it was in one file. But when I separated the program, I kept getting random occurrences for the destructor of one of the classes which was declared in the header file.

I have a static variable in my header to count the number of objects of a particular class. Whenever I construct the object I increment this variable. Then in my destructor I subtract 1 from that variable, check if it’s 0 (meaning it’s the last object) and do something. The value seems to be off sometimes, I’m not sure why. I do have random calls in my application but I don’t see why that would effect what I have described above, thanks. Any help or insight is appreciated!

[Update]: have a base class, which contains the destructor.. which is implemented in the header, then I have two derived classes, which in their constructor increment the static var.. so what can I do?

What I am trying to do is the following: In my header I have this:

class A { public:     virtual ~A() {         count --;         if (count == 0) { /* this is the last one, do something */ }     }  class B : public A { public:     B(); } 

Then in Class B I have

B::B() {      count++; } 

Where can I define count so I don’t get misleading counts? Thanks.

  • 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. 2026-05-10T22:38:45+00:00Added an answer on May 10, 2026 at 10:38 pm
    class A { public:     virtual ~A() {         count --;         if (count == 0) { // this is the last one, do something }     } protected:     static int count; };  class B : public A{ public: B(); }; 

    And then, in one and only one of your source files you need to put the following. It should go in the source file that contains the code for class A.

    int A::count(0); 

    You need to do this, because the header file declared that there was going to be a variable named count, but didn’t allocate any storage for it. If you didn’t put it in a source file, the linker would complain that it wasn’t found.

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

Sidebar

Ask A Question

Stats

  • Questions 91k
  • Answers 91k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Try something like this: private T PopulateObject<T>(T dataObj, System.Data.DataRow dataRow,… May 11, 2026 at 6:14 pm
  • Editorial Team
    Editorial Team added an answer Format those long numbers as strings by putting a '… May 11, 2026 at 6:14 pm
  • Editorial Team
    Editorial Team added an answer You do not need to cast. LinkedList implements List so… May 11, 2026 at 6:14 pm

Related Questions

I wrote an application recently, which relies on OpenID for authentication. A lot of
I have a large collection of documents scanned into PDF format, and I wish
When I realized I needed to create an index for approximately 50 XHTML pages,
I'm working on kernel design, and I've got some questions concerning paging. The basic

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.