mercredi 1 janvier 2020

Creating multiple instances of a subclass, with only one instance of a superclass

I'm trying to implement the Flyweight pattern, but I'm not quite sure how inheritance works, and so I'm not quite sure how this pattern works.

Let's say I have a superclass that holds all the "heavy" information - textures, etc., the instrinsic information (the one that never changes).

class Block{
public: //for the sake of the example
Texture tex; 
etc.
};

And I have the light class with data that changes:

class Block_light : public Block {
public:
int posX, posY, posZ;
int color;
etc.
};

So, if I in main create n Block_light, will I be also creating n Block or will they all be tied to one instance?

 int main(){
 std::vector<Block_light> blocks(n);
 return 0;
 };

Did I create n Block_light and 1 Block or n Block_light and n Block? If it's the latter, how can I make it so that it only uses one instance?

Aucun commentaire:

Enregistrer un commentaire