What's the best signature for clone() in C++?

后端 未结 8 1473
别那么骄傲
别那么骄傲 2020-12-05 10:41

As Scott Myers wrote, you can take advantage of a relaxation in C++\'s type-system to declare clone() to return a pointer to the actual type being declared:

         


        
8条回答
  •  鱼传尺愫
    2020-12-05 11:04

    I think the function semantics are so clear in this case that there is little space for confusion. So I think you can use the covariant version (the one returning a dumb pointer to the real type) with an easy conscience, and your callers will know that they are getting a new object whose property is transferred to them.

提交回复
热议问题