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 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 61k
  • Answers 61k
  • 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
  • added an answer How can I install to a path under my home… May 11, 2026 at 9:47 am
  • added an answer I saw this post and thought I'd write you a… May 11, 2026 at 9:47 am
  • added an answer This line here: void Processor::Process(MyClass ms, const std::string& key) Try… May 11, 2026 at 9:47 am

Related Questions

I wrote a program out, which was all in one file, and the methods
I'm just starting out writing trying to write a simple program in C and
I wrote a program in C with Ubuntu Linux and now I need to
I wrote a program which includes writing and reading from database. When I run
I wrote a program using AutoIT to fetch information from a number of websites
I wrote a quick program in python to add a gtk GUI to a
I wrote a sample program at http://codepad.org/ko8vVCDF that uses a template function. How do
I wrote a Java program to add and retrieve data from an MS Access.
I wrote a simple Windows Forms program in C#. I want to be able
I've been playing with Python for a while and wrote a little program to

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.