1 特殊成员函数
一个类,当只有数据成员时,C98 的编译器会隐式的产生四个函数:缺省构造函数,析构函数,拷贝构造函数 和 拷贝赋值算子,称为特殊成员函数
1 2 3 4 | class DataOnly { private : int data_; }; |
C++11 中,还有额外的两个特殊成员函数:移动构造函数 和 移动赋值算子
1 2 3 4 5 6 7 8 9 10 11 | class DataOnly { public : DataOnly () // default constructor ~DataOnly () // destructor DataOnly ( const DataOnly & rhs) // copy constructor DataOnly & operator=( const DataOnly & rhs) // copy assignment operator DataOnly ( const DataOnly && rhs) // C++11, move constructor DataOnly & operator=(DataOnly && rhs) // C++11, move assignment operator }; |
2 禁止编译器合成函数
作为开发者,如果不想让用户使用某个成员函数,不声明即可;但对于特殊成员函数,则是另一种情况
例如,设计一个树叶类
1 2 3 | class LeafOfTree{ // ... }; |
莱布尼茨说过,"世上没有两片完全相同的树叶" (Es gibt keine zwei Blätter, die gleich bleiben),因此,对于一片独一无二的树叶,下面的操作是错误的:
1 2 3 4 | LeafOfTree leaf1; LeafOfTree leaf2; LeafOfTree leaf3(leaf1); // attempt to copy Leaf1 — should not compile! Leaf1 = Leaf2; // attempt to copy Leaf2 — should not compile! |
此时,需要避免使用 "拷贝构造函数" 和 "拷贝赋值算子"
2.1 私有+不实现
C++98 中,可声明这些特殊成员函数为私有型 (private),且不实现该函数,具体如下:
1 2 3 4 5 | class LeafOfTree{ private : LeafOfTree( const LeafOfTree& ); // not defined LeafOfTree & operator=( const LeafOfTree& ); // not defined }; |
程序中如果调用了 LeafOfTree 类的拷贝构造函数 (或拷贝赋值操作符),则在编译时,会出现链接错误 (link-time error)
为了将报错提前到编译时 (compile time),可增加了一个基类 Uncopyable,并将拷贝构造函数和拷贝赋值算子声明为私有型,具体可参见 <Effective C++_3rd> item 6
在谷歌 C++ 编码规范中,使用了一个宏定义来简化,如下所示:
1 2 3 4 5 | // A macro to disallow the copy constructor and operator= functions // This should be used in the priavte:declarations for a class #define DISALLOW_COPY_AND_ASSIGN(TypeName) \ TypeName( const TypeName&); \ TypeName& operator=( const TypeName&) |
2.2 delete 关键字
C++11 中,可在 "禁止使用" 的特殊成员函数声明后加 "= delete",而需要保留的加 "= default" 或者不采取操作
1 2 3 4 5 6 7 8 | class LeafOfTree{ public : LeafOfTree() = default ; ~LeafOfTree() = default ; LeafOfTree( const LeafOfTree&) = delete ; // mark copy ctor or copy assignment operator as deleted functions LeafOfTree & operator=( const LeafOfTree&) = delete ; }; |
3 delete 的扩展
C++11 中,delete 关键字可用于任何函数,不仅仅局限于类成员函数
3.1 函数重载
在函数重载中,可用 delete 来滤掉一些函数的形参类型,如下:
1 2 3 4 | bool IsLucky( int number); // original function bool IsLucky( char ) = delete ; // reject chars bool IsLucky( bool ) = delete ; // reject bools bool IsLucky( double ) = delete ; // reject doubles and floats |
这样在调用 IsLucky 函数时,如果参数类型不对,则会出现错误提示
1 2 3 | if (IsLucky( 'a' )) … // error ! call to deleted function if (IsLucky( true )) … // error ! if (IsLucky(3.5)) … // error ! |
3.2 模板特化
在模板特例化中,也可以用 delete 来过滤一些特定的形参类型。
例如,Widget 类中声明了一个模板函数,当进行模板特化时,要求禁止参数为 void* 的函数调用。
如果按照 C++98 的 "私有不实现" 思路,应该是将特例化的函数声明为私有型,如下所示:
1 2 3 4 5 6 7 8 | class Widget { public : template < typename T> void ProcessPointer(T* ptr) { … } private : template <> void ProcessPointer< void >( void *); // error! }; |
问题是,模板特化应该被写在命名空间域 (namespace scope),而不是类域 (class scope),因此,该方法会报错。
而在 C++11 中,因为有了 delete 关键字,则可以直接在类域外,将特例化的模板函数声明为 delete, 如下所示:
1 2 3 4 5 6 7 8 | class Widget { public : template < typename T> void ProcessPointer(T* ptr) { … } }; template <> void Widget::ProcessPointer< void >( void *) = delete ; // still public, but deleted |
这样,当程序代码中,有调用 void* 作形参的 ProcessPointer 函数时,则编译时就会报错。
小结
C98 中这种 "私有不实现"的方式,其实是模仿 C11 中的 delete 功能,本身有一些局限:在类外不起作用;在类内有时不起作用;有时可能直到链接时才会起作用。总之,没有 delete 好用。
因此建议如下:
1) Prefer deleted functions to private undefined ones
2) Any function may be deleted, including non-member functions and template instantiations
参考资料:
《C++ Primer》5th chapter 13 Copy Control
《Effective C++》3rd item 5 , item 6
《Effective Modern C++》 item 11 , item 17
原文链接: http://www.cnblogs.com/xinxue/
专注于机器视觉、OpenCV、C++ 编程
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人