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

后端 未结 8 1360
别那么骄傲
别那么骄傲 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 10:58

    It depends on your use case. If you ever think you will need to call clone on a derived object whose dynamic type you know (remember, the whole point of clone is to allow copying without knowing the dynamic type), then you should probably return a dumb pointer and load that into a smart pointer in the calling code. If not, then you only need to return a smart_ptr and so you can feel free to return it in all overrides.

提交回复
热议问题