Is there a way to share a variable between instances of a class, but not between instances of the class containing it?

I have a class called Monster that inherits from class Entity, which has a variable called size. I want every Monster to have the same size by definition, but I haven't found any good way of doing that.

I wanted to know if there's maybe some kind of keyword that lets you make a shared variable between all instances of a class. It saves the variable or function in memory like it belongs to one higher scope, leaves the variable accessible only to the scope in which it was created and can replace a variable of a parent class.

For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Entity
{
protected:
 sf::Vector2f position;
 sf::Vector2f size;
};

class Monster
{
 shared size;

public:
 shared void init(const sf::Vector2f size)
 {
  this->size = size;
 }

 Monster(){}
 Monster(const sf::Vector2f& position)
 {
  this->position = position;
 }
};

int main()
{
 Monster::init(sf::Vector2f(15, 20));

 Monster m1(sf::Vector2f(55, 30));
 Monster m2(sf::Vector2f(67, 73));

 return 0;
}


Thanks for the help
Are you looking for static class members?

You declare a static class member in the class declaration. Because there will be one instance of that value, rather than one for each instance, you must also create the actual one instance of the static value. See code below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>

using std::cout;
using std::endl;

class Base
{
    public:
    int size;
    
    
};


class Monster : public Base
{

public:
double position;

 static int size; // This will be shared by all instances of Monster

 Monster() {}
 Monster(const double baseSize)
 {
     Base::size = baseSize;
 }
};

int Monster::size = 7; // create the shared value

int main()
{

 Monster m1(12);
 Monster m2(14);
 
 cout << "m1 size is " << m1.size << "  , m1 base size is " << m1.Base::size <<  endl;
 cout << "m2 size is " << m2.size << "  , m2 base size is " << m2.Base::size <<  endl;
 return 0;
}


WHile I'm here, any time you find yourself writing this-> , don't. Go back and rename your parameter or class member variable so that you're not using the same name for different things.
Last edited on
Thanks for the answer, though the static keyword is not what I'm looking for.
Using this system, each Monster instance still contains a different copy of size. Which needlessly takes up memory.
I'm also looking for a way to make it so that the variable is saved as if it belongs to one higher scope, not necessarily a global scope, for when I want to share variables between instances of nested classes.
Last edited on
Which needlessly takes up memory.


Well, let's see here. They've all got an individual Base::size object, which is an int. So either four bytes, or eight bytes. So if you had a million of these, you'd be using 8 million bytes, call it 8 MB, so on a modern system with 8GB of RAM, you'd be looking at having used one thousandth of your memory. Are you planning to run this program on something only a few MB of memory? I suspect not.

Are you expecting a million of these? Because that won't be any problem whatsoever. Ten million? Also fine. Don't waste your time worrying about things that aren't actually any kind of problem whatsoever.

I'm also looking for a way to make it so that the variable is saved as if it belongs to one higher scope,

I don't understand. What does "saved" mean?

I think that the static keyword is what you're looking for. It meets your needs perfectly. All the Monster objects share a size value. That's what you wanted. I don't know what you're getting at when you talk about higher scopes.
Last edited on
The problem is that you're saying that every Entity has an individual size, except that isn't really true: some have very specific sizes. So handle it with a virtual method:
How about:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Entity {
protected:
    virtual sf::Vector2f &getSize();
};

class EntityWithSize : public Entity {
protected:
    // These entities have an instance-specific size
    sf::Vector2f size;
    sf::Vector2f &getSize() {return size; }
};

class Monster: public Entity {
protected:
    // All monsters have one size.
    static sf::Vector2f size = 22;  // or whatever;
};


sf::Vector2f &getSize() { return size; }
};
Topic archived. No new replies allowed.