Pass by value or reference, to a C++ constructor that needs to store a copy?

前端 未结 8 2169
刺人心
刺人心 2020-12-11 19:50

Should a C++ (implicit or explicit) value constructor accept its parameter(s) by value or reference-to-const, when it needs to store a copy of the argument(s) in its object

相关标签:
8条回答
  • 2020-12-11 20:39

    Honestly, for this case, the best option is making your constructor inline, provided bar's constructor does not throw. Then just pass by reference.

    Also, as you suspected, your article does not apply here.

    0 讨论(0)
  • 2020-12-11 20:40

    Look this question.

    Use const T & arg if sizeof(T)>sizeof(void*) and use T arg if sizeof(T) <= sizeof(void*). All fundamental types should be the exception to this rule

    0 讨论(0)
提交回复
热议问题