Jump to content
  • Advertisement
Sign in to follow this  
Angex

Sharing an instance variable between all instances of a class (C++).

This topic is 5231 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

What I want to do is have a pointer in a super class that once initialised has the same address for all instances of it's subclasses. I thought that I could use the static keyword but I keep getting unresloved external symbol errors during the linking phase. Do demonstrate what I mean I wrote a simple example in java:
public class Gremlin {
    protected static int numberOfGremlins = 0;

    public Gremlin() {
        System.out.println("There is " + (++numberOfGremlins) + " gremlin(s) !\n");
    }
}



public class EvolvedGremlin extends Gremlin {
    protected static int numberOfEvolvedGremlins = 0;

    public EvolvedGremlin() {
        super();
        System.out.println("There is " + (++numberOfEvolvedGremlins) + " evolved gremlin(s) !");
        System.out.println("There is " + numberOfGremlins + " gremlin(s) altogeather !\n");
    }

    public void countGremlins() {
        System.out.println("I counted " + numberOfGremlins + " gremlin(s) !");
    }
}

EvolvedGremlin inherits the nuberOfGremlins from Gremlin, but the value is the same for every instance of EvolvedGremlin. So if I created two EvolvedGremlin objects, and two Gremlin objects, and then got an EvolvedGremlin to count the number of Gremlins the output would be:
There is 1 gremlin(s) !

There is 1 evolved gremlin(s) !
There is 1 gremlin(s) altogeather !

There is 2 gremlin(s) !

There is 3 gremlin(s) !

There is 4 gremlin(s) !

There is 2 evolved gremlin(s) !
There is 4 gremlin(s) altogeather !

I counted 4 gremlin(s) !

However I am at a loss as to how this could be done in C++ ?

Share this post


Link to post
Share on other sites
Advertisement
Ok, say you declare it in a header file:
class Gremlin {
static int numberOfGremlins;
// And so on...

You'll also need to define it somewhere, the best place would be in the source file corresponding to the header:
int Gremlin::numberOfGremlins=0;

Share this post


Link to post
Share on other sites
What is the exact error you are getting? Using static is correct here. Something like:


class Gremlin
{
public:
Gremlin();
protected:
static int numberOfGremlins;
};

int Gremlin::numberOfGremlins = 0;

Gremlin::Gremlin()
{
numberOfGremlins++;
}





--------------
Andrew

Share this post


Link to post
Share on other sites
Thanks guys, I did not realise you needed to add a definition within the source code, so I only had the declaration in the header file; which explains the error.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!