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

Author Topic: Quick reference question.  (Read 1907 times)

0 Members and 1 Guest are viewing this topic.

Contadotempo

  • Full Member
  • ***
  • Posts: 167
  • Firelink Shrine
    • View Profile
Quick reference question.
« on: April 10, 2011, 07:10:52 pm »
Hello, I'm having a bit of trouble thinking of a way to pass an sf::RenderWindow object to another class.

In the main I have object "Window"
Code: [Select]
int main
{
sf::RenderWindow Window(sf::VideoMode(1024,768,32),"Default");
[...]
}


And I want to pass this object to a class "cTest".
I tried it like this:
Code: [Select]
class cTest
{
sf::RenderWindow& m_Window;
cTest(sf::RenderWindow& Window):m_Window(Window);
};



Final Code:
Code: [Select]
class cTest
{
sf::RenderWindow& m_Window;
cTest(sf::RenderWindow& Window):m_Window(Window);
};

int main
{
sf::RenderWindow Window(sf::VideoMode(1024,768,32),"Default");
[...]
        cTest otest(Window);
        [...]
}


I does seem too work but I can't stop shaking the feel that this isn't the correct way to do this. Could someone enlighten me please?

Thanks in advance  :)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Quick reference question.
« Reply #1 on: April 10, 2011, 07:19:20 pm »
This is correct. You don't use any pointers, but a reference. You just have to make sure that the referenced window remains valid as long as it is used by cTest. But when declared in main() like here, this shouldn't be a problem.

And please stop with those "c" class prefixes, SFML demonstrates that it is perfectly possible without :)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Contadotempo

  • Full Member
  • ***
  • Posts: 167
  • Firelink Shrine
    • View Profile
Quick reference question.
« Reply #2 on: April 10, 2011, 07:23:03 pm »
Thanks for the quick reply :), I've fixed the title just then.

Don't worry about the "c" prefixes, I only tend to use them on simplified code but I'll stop from now on! Thanks!

 

anything