Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Forgive what may be a dumb question (I'm not a C++ user), but why should dothing() accept a reference to struct? Why is this better than just making it accept a struct like

void dothing(const dothingArgs args);

and then just passing it an anonymous struct that only briefly sits on the stack when you call

dothing({ .arg1 = 1, .arg2 = someOtherThing });

A reference would make sense if you already have a struct sitting somewhere else in your program, but if you're putting together the struct at the time you call the function, wouldn't it make more sense to just pass by value?



Because otherwise the struct will be copied when the function is called.

Though your question is kind of getting at r-values and move semantics, which is a solution to prevent copying the temp value both when calling the function and in its execution. You can read about it because it will take too long to explain it here.


Thanks, I'll read about that




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: