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

Author Topic: Why does RenderWindow::GetView() return a const View&?  (Read 2613 times)

0 Members and 1 Guest are viewing this topic.

quasius

  • Full Member
  • ***
  • Posts: 166
    • View Profile
Why does RenderWindow::GetView() return a const View&?
« on: August 22, 2008, 06:17:00 am »
RenderWindow::GetView returns a const View& and RenderWindow::SetView takes a const View&.
I'm just wondering why GetView returns a const View?  You can't directly modify it since it's const and you can't receive it into a local variable and pass it back since SetView takes a ref which will be immediately trashed when the scroll function returns.
So the only current solution I see is too store a class View variable to pass the reference of.  This seems a bit odd and perhaps slightly wasteful.
Am I missing something here?  (It's late and I need sleep, so I might be!)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Why does RenderWindow::GetView() return a const View&?
« Reply #1 on: August 22, 2008, 09:11:19 am »
When you give a view to the window, it's for applying it only, ie. the window only has a read access to it. So its stores and return it as a const object.

If there was no const, you couldn't pass a const view to the window, then a lot of users would come here and complain "hey, why can't I pass my const view ? the window won't modify it, is it ?" ;)

A small example :
Code: [Select]
class MyScene
{
public :

    void Draw(sf::RenderWindow& Window) const
    {
        Window.SetView(myView); // --> COMPILE ERROR

        Window.Draw(...);
    }

private :

    sf::View myView;
};


The ownership of the view is external, you have to handle it by yourself. RenderWindows just use your custom views, they are not meant to give full access to them.

By the way, if you don't need several views, you can just use the default view, which can be accessed for writing from the window.
Laurent Gomila - SFML developer

 

anything