SFML community forums

Help => Window => Topic started by: Christopher Jozwiak on June 28, 2016, 02:41:16 am

Title: Passing in reference.
Post by: Christopher Jozwiak on June 28, 2016, 02:41:16 am
I'd like to start off by saying I have read a C++ book. 
That said, I'd had a working example of using a reference to pass in the window to different files.  However the problem I had was having to pass in sf::RenderWindow &mWindow to pretty much every function that needed the window.  The problem lied when I tried to split off functions into different files I couldn't get the actual copy of the window I needed.  The deeper down the hierarchy I got it seemed I was getting lost in what I was trying to do.  It was a logic problem but I'm contemplating on how I could use one function to get a copy of the window.  Just looking for advice. =)
Title: Re: Passing in reference.
Post by: Fitzy on June 28, 2016, 02:56:38 am
const sf::RenderWindow& getWindow() const
{
   return mWindow;
}
 

That function won't allow you to actually edit the window instance since it's returning a constant reference to mWindow. If you want to edit the window when you get the reference to it, then:

sf::RenderWindow& getWindow() const
{
   return mWindow;
}
 

The const at the end of function name means you won't be able to edit mWindow within the getWindow() function, which is fine because all you're doing is "return mWindow;". There are deeper meanings to what const does but I will advise you to look up C++ Const Correctness (http://www.cprogramming.com/tutorial/const_correctness.html), if you want a better understanding of how it works.

Hope this helps.