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.
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.
#include <iostream>
using std::cout;
using std::endl;
class Base
{
public:
int size;
};
class Monster : public Base
{
public:
double position;
staticint size; // This will be shared by all instances of Monster
Monster() {}
Monster(constdouble baseSize)
{
Base::size = baseSize;
}
};
int Monster::size = 7; // create the shared valueint 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.
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.
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.
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;
};