Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Passing in reference.  (Read 2193 times)

0 Members and 1 Guest are viewing this topic.

Christopher Jozwiak

  • Newbie
  • *
  • Posts: 48
    • View Profile
    • Blog
    • Email
Passing in reference.
« 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. =)
Noob C++ Programmer.

Fitzy

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
Re: Passing in reference.
« Reply #1 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, if you want a better understanding of how it works.

Hope this helps.
« Last Edit: June 28, 2016, 03:00:49 am by Fitzy »