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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:56:40+00:00 2026-05-26T05:56:40+00:00

I would like to have a static member variable keep track of the amount

  • 0

I would like to have a static member variable keep track of the amount of objects that have been made. Like so:

class test{
    static int count = 0;
    public:
        test(){
            count++;

        }
}

That doesn’t work because, according to VC++, a member with an in-class initializer must be constant. So I looked around and apparently you’re supposed to do:

test::count = 0;

Which would be great, but I want count to be private.

edit:
Oh boy, I just realized I need to do:

int test::count = 0;

I had seen something just do test::count = 0, so I assumed you wouldn’t have to declare type again.

I’d like to know if there’s a way to do this inside the class though.

edit2:

What I’m using:

class test{
    private:
        static int count;
    public:
        int getCount(){
            return count;
        }
        test(){
            count++;

        }
}
int test::count=0;

Now it’s saying: 'test' followed by 'int' is illegal (did you forget a ';'?)

edit3:

Yup, forgot a semicolon after the class definition. I’m not used to having to do that.

  • 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-26T05:56:41+00:00Added an answer on May 26, 2026 at 5:56 am

    Put

    static int count;
    

    In your header in the class definition, and

    int test::count = 0;
    

    In the .cpp file. It will still be private (if you leave the declaration in the header in the private section of the class).

    The reason you need this is because static int count is a variable declaration, but you need the definition in a single source file so that the linker knows what memory location you’re referring to when you use the name test::count.

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

Sidebar

Related Questions

I have a static class that I would like to raise an event as
I have a class that only has static members. I would like to register
I have a class in which I would like to have a static member
I have a public static class in which I would like to have a
I have a static method in my code that I would like somehow to
I have a final non-static member: private final HashMap<String,String> myMap; I would like to
I would like to have our web applications pull static content (css, js, images)
I have a page where I would like it to remain static after refresh
I have a problem that I would like have solved via a SQL query.
I have a situation where a helper class with a static DBContext member is

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.