-2

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

PeakGen
  • 21,894
  • 86
  • 261
  • 463
  • 5
    I want to recommend a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), because they cover access modifiers and static data members. – R. Martinho Fernandes Dec 04 '12 at 14:03
  • Where are you tried to access the static member? – Hamlet Hakobyan Dec 04 '12 at 14:05
  • inside main I tried "GameObject::counter" . But I can't get it in that way. In other words, intellisense of visiual studio is not showing it – PeakGen Dec 04 '12 at 14:08
  • People that do downvotes, please obey the right of making mistakes. If not, please prove you have never done a mistake – PeakGen Dec 04 '12 at 14:34

3 Answers3

2

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).

Mark Ingram
  • 71,849
  • 51
  • 176
  • 230
  • My lecturer notes says defining it inside header will lead to "Multiple Initialization" error – PeakGen Dec 04 '12 at 14:12
  • 1
    He's wrong, it's as easy as typing `static int s_counter = 0;`. Even if the compiler doesn't support that, you can declare the variable in the header, then implement it in the cpp file. e.g. `static int s_counter;`, then in the cpp, `int GameObject::s_counter = 0;`. – Mark Ingram Dec 04 '12 at 14:14
  • OK. Thank you :) Now I got 2 ways of doing it :) – PeakGen Dec 04 '12 at 14:16
0

In your code, counter is not a static member of your class, but just a good old global variable. You can read up on how to declare static member variables anywhere on the net, but here are some snippets:

GameObject.h

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

private:
    int id;

// static members
public: 
    static int counter; // <<<<<<<<<<< declaration
};

ameObject.cpp

#include "GameObject.h"

int GameObject::counter = 0; // <<<<<<<<<<< instantiation


GameObject::GameObject(void)
{
    counter++;
}

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


GameObject::~GameObject(void)
{
}

Main.cpp

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

using namespace std;

int main()
{
    GameObject obj2(45);
    cout << "version one: " << obj2.counter << endl;

    // second version:
    cout << "version two: " << GameObject::counter << endl;

    system("pause");
    return 0;
}

Afaik accessing static members on an instance of that class should work fine, but it gives the wrong impression, you should prefer version two.

SvenS
  • 795
  • 7
  • 15
-1

Never mind, I did it with a getter method. Closing thread

PeakGen
  • 21,894
  • 86
  • 261
  • 463
  • after got to know that defining static inside header will not lead to "Multiple Initializations" error, I decided to shift back to Mark Ingram's answer – PeakGen Dec 04 '12 at 14:31