在学习C++的过程中,我偶然发现了文章WritingCopyConstructorsandAssignmentOperators它提出了一种机制来避免复制构造函数和赋值运算符之间的代码重复。为了总结/复制该链接的内容,建议的机制是:structUtilityClass{...UtilityClass(UtilityClassconst&rhs):data_(newint(*rhs_.data_)){//nothinglefttodohere}UtilityClass&operator=(UtilityClassconst&rhs){////Leavesalltheworktotheco
在学习C++的过程中,我偶然发现了文章WritingCopyConstructorsandAssignmentOperators它提出了一种机制来避免复制构造函数和赋值运算符之间的代码重复。为了总结/复制该链接的内容,建议的机制是:structUtilityClass{...UtilityClass(UtilityClassconst&rhs):data_(newint(*rhs_.data_)){//nothinglefttodohere}UtilityClass&operator=(UtilityClassconst&rhs){////Leavesalltheworktotheco