简体   繁体   中英

Sharing class variables between instances without using static

I have a hierarchy that goes like this:

       Entity
   /     |     \
Player Enemy Weapon
        /\        /  \
    Slime Fly  Sword  Gun

etc. And some code:

class Entity {
public:
    void LoadModel();
protected:
    Model* model;
}

Each Entity class has its own model, or at least, that's how I would like it to work--the problem is that, if I were to make models static to a class, I would have repeated code in every derived class to define the static model, then have a virtual function in every class to return it so that the base Entity class can use its load function, etc.

So what I would like to do is to be able to use the base class's LoadModel() function to load a model for the specific derived class. Is this possible? How would I achieve this?

Inside the code of the derived class you can call the base class with

Derived d;
d.base::LoadModel();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM