简体   繁体   中英

C++ function parameter as reference

I have a question. Can anyone tell me why someone would declare parameter r like that ?

Whats the difference between Record& r and Record(&r) ?

QDataStream& operator>>(QDataStream &s, Record(&r))
{
}

Thanks :)

Record(&r) and Record &r are identical type declarations. I don't know why somebody would include the parentheses except for stylistic reasons. Perhaps it's some cruft left over from a refactoring?

As Richard said Record (&r) and Record &r are equivalent. So the important difference is how effectively they communicate the intent of programmer. I personally prefer Record& r over Record &r because I like to separate the type from the name. That said, I would avoid the Record (&r) because it looks so similar to the syntax for function types. For example, Record (&r)() would declare r as a reference to a function such as the following.

Record f() { }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM