How does this work?

Structure
 ``1234567`` ``````struct free_throws { std::string name; int made; int attempts; float percent; };``````

Function definition
 ``1234567`` ``````free_throws & accumulate(free_throws & target, const free_throws & source) { target.attempts += source.attempts; target.made += source.made; set_pc(target); return target; }``````

Main
 ``1234`` ``````free_throws dup; free_throws four = {"Whily Looper", 5, 9}; free_throws five = {"Long Long", 6, 14}; accumulate(dup,five) = four; ``````

How exactly does accumulate(dup,five) = four; work? It seems like it is assigning a free_throws value to a function which does not exactly make logical sense.
`accumulate` return a value, in this case: `free_throws &`

so if there is` return target` and target is reference to `dup`,
then line `accumulate(dup,five) = four;` is assigning` struct four` values to `struct dup`.

----
so if you have function that return value, treat it, as value it returns. It is the best seen in IDE using code completion.
 ``123456`` ``````std::string func() { std::string asd("asdasd"); return asd; }``````

and when using this function you have access to all all std::string asd methods:
 ``1234567`` ``````int main () { asd().c_str()[0] = 's'; //asd().at[1] ... [etc] return 0; }``````

Last edited on
Topic archived. No new replies allowed.