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

Author Topic: [SOLVED] Question regarding RenderWindow's c-tor  (Read 1676 times)

0 Members and 1 Guest are viewing this topic.

Spacelord

  • Newbie
  • *
  • Posts: 2
    • View Profile
[SOLVED] Question regarding RenderWindow's c-tor
« on: August 30, 2014, 02:06:58 am »
Hello everyone!

A new SFML user here. I was playing around with sf::RenderWindow as a member variable inside a class and noticed something; when I initialize window using init lists, it works:

SharpShooter::SharpShooter() : window(sf::VideoMode(680,480), "Rendering Area") { }

However, if I declare it inside the body of the c-tor like so:

SharpShooter::SharpShooter()  
{
window(sf::VideoMode(680,480), "Rendering Area");
 }

I get the following error;

Quote
error: no match for call to ‘(sf::RenderWindow) (sf::VideoMode, const char [12])’

FYI, window is neither a const member nor a reference.

This is might be more of a C++ question but I was curious and wanted to ask about it

Thanks in advance.
« Last Edit: August 30, 2014, 07:29:47 pm by Spacelord »

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Question regarding RenderWindow's c-tor
« Reply #1 on: August 30, 2014, 02:20:14 am »
If I remember how C++ works, then...

Class members can only be initialized in the initialization list of the class' constructor(s).  If you do not initialize the "window" member there, then it gets initialized using the default constructor, and by the time you enter the body of the class' constructor, you've already missed your chance to call one of window's other constructors.  Then, when it sees you trying to call "window" as if it was a function, it complains that it can't find a function matching that signature.  This is standard C++ stuff.

If for some reason you don't want to construct it in the initialization list, sf::Window and sf::RenderWindow do offer a create() method which basically lets you reconstruct it whenever you want.  This "two-stage initialization" is a feature that SFML and many other libraries deliberately choose to provide.
« Last Edit: August 30, 2014, 02:24:58 am by Ixrec »

Spacelord

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Question regarding RenderWindow's c-tor
« Reply #2 on: August 30, 2014, 07:29:23 pm »

Got it now; what I was doing basically in the second case is calling a function called window that would return void and not the constructor (http://www.parashift.com/c++-faq/empty-parens-in-object-decl.html).

Thanks for your help Ixrec! :)

 

anything