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

Author Topic: [Solved] I don't get this code snippet from the book SFML Game Development  (Read 2684 times)

0 Members and 1 Guest are viewing this topic.

Flaco

  • Newbie
  • *
  • Posts: 36
  • Glory to Arztotzka!
    • View Profile
    • Core Unit
    • Email
Greetins y'all !  :D

Well; I'm actually reading SFML Game Development and in the chapter 4, sub-chapter "Handling Player Input" there's something I don't understand.

Here's the code (I explain what I don't understand below):

(click to show/hide)

Where the class Aircraft is (basically) a scene node handling its sf::Sprite, its type, etc...

So, what I don't get here is, if derivedAction() expects two parameters (SceneNode& and sf::Time), why in this snippet it just receives one argument (of type SceneNode&, I assume, since the code changes further in the chapter I can't really be sure it is one or just some "basic key word so it's simpler and lighter").

So yes, I know, as I just said, the code changes so it may just be an example but could somebody explain me if this code would work and (most of all) why?
I have read the documentation about std::function on cppreference and as I understood, this code is not legal.

Maybe I'm just taking a simple example too seriously, if so; I'm sorry to have bothered you. :D

Thank you !
« Last Edit: June 30, 2014, 02:00:48 pm by Flaco »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: I don't get this code snippet from the book SFML Game Development
« Reply #1 on: June 29, 2014, 07:36:36 pm »
It looks to me as though derivedAction() expects one parameter - a function - and AircraftMover() is the function that expects the two parameters that you specified.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Flaco

  • Newbie
  • *
  • Posts: 36
  • Glory to Arztotzka!
    • View Profile
    • Core Unit
    • Email
Re: I don't get this code snippet from the book SFML Game Development
« Reply #2 on: June 29, 2014, 07:51:50 pm »
Oh right, looks like I didn't fully understood the uses of std::function. :D Thank you!
But one mystery remains in my case... what is the purpose of the two parameters putted in the std::function declaration?

/* ... */

std::function<void(SceneNode& node, sf::Time dt)> foo;

/* ... */
 

I think I shall RTFM a second time. :)

EDIT: Read it on a bunch of sources, still didn't found a function wrapped by a std::function having "its own" parameters (I thought the <void(int, int)> were the wrapped element's parameters and return type).
« Last Edit: June 29, 2014, 08:17:13 pm by Flaco »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: I don't get this code snippet from the book SFML Game Development
« Reply #3 on: June 29, 2014, 08:22:08 pm »
I think that derivedAction returns two values. Does the Aircraft class include SceneNode and sf::Time values?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: I don't get this code snippet from the book SFML Game Development
« Reply #4 on: June 29, 2014, 08:28:14 pm »
Why don't you read a tutorial about std::function? The book, nor this forum, won't teach you what it is and how it works ;)

std::function wraps a function, and the template parameter is the signature of the wrapped function.

void func(int, std::string);

std::function<void(int, std::string)> foo = func;

foo(5, "hello"); // calls func(5, "hello")
Laurent Gomila - SFML developer

Flaco

  • Newbie
  • *
  • Posts: 36
  • Glory to Arztotzka!
    • View Profile
    • Core Unit
    • Email
Re: I don't get this code snippet from the book SFML Game Development
« Reply #5 on: June 29, 2014, 08:45:27 pm »
Hello Laurent, thanks for your help.
I will read a tutorial about std::function, you are right. :)

I "kinda" see how it works; it was the parenthesis after the name of the std::function that confused me.
So, void(SceneNode& foo, sf::Time dt) is the lambda expression signature.
And derivedAction<Aircraft> is the derivedAction's parameter type (since Function is a generic type).

I get it, thank you Laurent, thank you Golden Eagle, now, it's time to read some tutorial. :D

(Good evening/day to you!)

 

anything