Give parent class a pointer to an object for all child classes?

I need every game object to have a pointer to my render window, but I want to do it in the base class so I don't have to keep doing it in every child class.

An example!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

class GameObject
{
protected:
sf::RenderWindow* window = renderWindow; //point "window" to my renderwindow in this class
public:
};

class Actor : public GameObject
{
//now can use window without having to point it to something in this class!

void Update()
{
if(window-> bla bla bla)
}
};
Last edited on
assign it in the constructor in the base class?

1
2
3
4
5
6
7
class GameObject
{
protected:
    GameObject() : window(renderWindow) {}
    sf::RenderWindow* window;
public:
};
Topic archived. No new replies allowed.