Is it safe to return a struct in C or C++?

后端 未结 12 1900
北海茫月
北海茫月 2020-12-04 07:55

What I understand is that this shouldn\'t be done, but I believe I\'ve seen examples that do something like this (note code is not necessarily syntactically correct but the

12条回答
  •  醉话见心
    2020-12-04 08:47

    It is not safe to return a structure. I love to do it myself, but if someone will add a copy constructor to the returned structure later, the copy constructor will be called. This might be unexpected and can break the code. This bug is very difficult to find.

    I had more elaborate answer, but moderator did not like it. So, at your expence, my tip is short.

提交回复
热议问题