Just change your WindowManager instance in World to make it a reference:
class World
{
public:
World(WindowManager& windowManager);
void createWorld();
std::vector<sf::RectangleShape> vecGrassTile;
std::vector<sf::RectangleShape> vecDirtTile;
private:
WindowManager& windowManager; //Now a reference
int tileFullSize;
sf::Vector2f tileSize;
sf::RectangleShape grassTile;
sf::RectangleShape dirtTile;
};
windowManager cannot be uninitialized, initializer lists let your initialize things before they get default-initialized:
World::World()
: windowManager(windowManager) //This is the initializer list
{
std::cout << "World constructor" << std::endl;
//here i initialize tile stuff, no problem, i cutted it out.
}
You will have to change the Core constructor:
Core::Core()
: firstMap(windowManager) //This ensures the windowManager is passed as a parameter to firstMap's constructor
{
std::cout << "Core constructor" << std::endl;
firstMap.createWorld();
windowManager.createWindow();
}
Also:
- Initializer lists are basically how you call the constructor for member variables. Since ALL members must be assigned something before the constructor is called, but it's default-initialized. If you need to pass a
parameter, reference, int, pointer, even the class itself, you have to use an initializer list.
- Always place the order of which members in the initializer list appear to the order of which they were declared (if you have more than one you need to initialize).
- This counts for anything, really, but C++ isn't Java, don't pass an object to another object (via reference or pointer) without being 100% sure the object holding the reference/pointer will die before the object the reference/pointer is referring/pointing to. If you create A, then B with a reference to A, then destroy A before B, you create undefined behavior (technically, if it's a pointer and B stops using A as soon as A dies, it's not undefined behavior, but that's a pretty specific scenario). If you don't know which will die first, A or B, then that's where shared pointers come in (A would have to be declared as a shared pointer instead of a scoped variable, though). Fortunately, as long as you declare member variables in the order of which they depend on each other, this isn't a problem since they both die alongside each other when the "host" object dies.