C++ 64 bit int: pass by reference or pass by value

后端 未结 5 1111
悲哀的现实
悲哀的现实 2020-12-31 05:46

This is an efficiency question about 64 bit ints. Assuming I don\'t need to modify the value of a \"int\" parameter, should I pass it by value or reference.

Assu

5条回答
  •  醉梦人生
    2020-12-31 06:14

    Use a little common sense,

    1. if the object requires a complex copy constructor, it's probably worth passing by reference (saying that - quite a lot of boost's objects are designed to be passed-by-value rather than reference simply because internal implementation is quite trivial) There is one odd one which I haven't really worked out, std::string, I always pass this by reference...

    2. If you intend to modify the value that is passed in, use a reference

    3. Else, PASS-BY-VALUE!

    Do you have a particular performance bottleneck with arguments to functions? Else, don't spend too much time worrying about which is the best way to pass...

    Optimizing by worrying about how an int is passed in is like pi**ing in the sea...

提交回复
热议问题