Inheritance problem

Hey

So I got a entity base class and then a projectile class that inherits the entity class. Now I got this problem that the entity class has a OnLoad function that loads a image for the entity.

This is all well and good if you got a object to load the image for. But with the projectiles i need to just load an image at the start of the program and then use the same image for all projectiles that I load during run time. I tried making the OnLoad function inside of projectile a static method but it complains that the OnLoad function in the entity class cant be overwritten.

Then i tried to rename it and see if that worked, but the OnLoad function inside of projectile needs to call Entitys OnLoad function and now it complains that it needs an object and cant use Entity::OnLoad.

Any tips, clues and/or comments?
Make Entity::OnLoad virtual. That means that the child classes can make their own versions of this function:

1
2
3
4
5
6
7
8
9
10
class Entity
{
protected:
    virtual void OnLoad() { do something; }
};

class Projectile : Entity
{
    void OnLoad() { do something else; }
};
Hi

It´s already a Virtual, but I cant make it static inside of projectile. Since I only want to load the projectile picture at the start of the program and not every time i create a new projectile during run time.
Use lazy initialization. Check if it is loaded, if it isn't, then load it, and in either case use it afterwards.
Thanks L B, I will try to figure out how to do that. That was nice, now i learned a new term. :)

Well I tried to get my head around this, but could anyone try and clarify how to do this? Do I do this inside the class? How do I make sure it only loads the first time?

I´m a noob so i tried in the class constructor but that doesnt work since it will run everytime a new object is created.
Last edited on
Topic archived. No new replies allowed.