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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:59:41+00:00 2026-06-15T07:59:41+00:00

Please have a look at the following code GameObject.h #pragma once class GameObject {

  • 0

Please have a look at the following code

GameObject.h

#pragma once
class GameObject
{
public: 
    GameObject(int);
    ~GameObject(void);


    int id;

private:
    GameObject(void);


};

GameObject.cpp

#include "GameObject.h"
#include <iostream>

using namespace std;

static int counter = 0;


GameObject::GameObject(void)
{
}

GameObject::GameObject(int i)
{
    counter++;
    id = i;
}


GameObject::~GameObject(void)
{
}

Main.cpp

#include <iostream>
#include "GameObject.h"

using namespace std;

int main()
{
    //GameObject obj1;
    //cout << obj1.id << endl;

    GameObject obj2(45);
    cout << obj2.id << endl;;
//  cout << obj2.counter << endl;

    GameObject obj3(45);
    GameObject obj4(45);
    GameObject obj5(45);


    //Cannot get Static value here
    //cout << "Number of Objects: " << GameObject
    //

    system("pause");
    return 0;
}

Here, I am trying to record how many instances have been created. I know it can be done by a static data member, but I can’t access it withing the Main method! Please help!

PS:

I am seeeking for a direct access, without a getter method

  • 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-15T07:59:42+00:00Added an answer on June 15, 2026 at 7:59 am

    Your static variable, counter cannot be accessed because it isn’t a member of GameObject. If you want to access the counter, you’ll need to do something like this:

    GameObject.h

    #pragma once
    
    class GameObject
    {
    public: 
        ...
        static int GetCounter();
        ...
    };
    

    GameObject.cpp

    int GameObject::GetCounter()
    {
        return counter;
    }
    

    Then you can access the variable like:

    cout << "Number of Objects: " << GameObject::GetCounter();
    

    Of course, there are other ways of accessing a static variable, such as making the counter variable a static member of your GameObject class (which I would recommend).

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

Sidebar

Related Questions

Please have a look at the following code GameObject.cpp #include GameObject.h GameObject::GameObject(void) { id
please have a look at the following code import java.util.ArrayList; import java.util.List; public class
Please have a look at the following code #include <iostream> using namespace std; int
Have a look at the following code snippet... File1.h void somefunc(int); File1.c #include File1.h
Please have a look at the following code. Main.cpp #include <iostream> #include Maximum.h using
Please have a look at the following code #include <iostream> using namespace std; int
Please have a look at the following code namespace Funny { class QuesionsAndAnswers {
Please have a look at following code : import java.util.ArrayList; import java.util.List; class Main{
Please have a look at the following code #include <iostream> #include <iomanip> #include <vector>
Please have a look at the following code DatabaseHandler.java public List getDetails(String name) {

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.