一个空类会生成哪些默认函数
2013-04-17 12:41
267 查看
/article/4871756.html
一个空类会生成哪些默认函数
定义一个空类class Empty { };
默认会生成以下几个函数
1. 无参的构造函数
Empty() { }
2. 拷贝构造函数
Empty(const Empty& copy) { }
3. 赋值运算符
Empty& operator = (const Empty& copy) { }
4. 析构函数(非虚)
~Empty() { }
这些函数只有在第一次使用它们的时候才会生成,他们都是inline并且public的。如果想禁止生成这些函数,可以将它们定义成private函数,如果有很多类都有这种需求,那么可以定义一个基类,然后让其他类继承这个类。下面是来自boost库的代码,任何继承了该类的类,都不能进行复制操作。也不能使用赋值运算符。
#ifndef BOOST_NONCOPYABLE_HPP_INCLUDED #define BOOST_NONCOPYABLE_HPP_INCLUDED namespace boost { // Private copy constructor and copy assignment ensure classes derived from // class noncopyable cannot be copied. // Contributed by Dave Abrahams namespace noncopyable_ // protection from unintended ADL { class noncopyable { protected: noncopyable() {} ~noncopyable() {} private: // emphasize the following members are private noncopyable( const noncopyable& ); const noncopyable& operator=( const noncopyable& ); }; } typedef noncopyable_::noncopyable noncopyable; } // namespace boost #endif // BOOST_NONCOPYABLE_HPP_INCLUDED
相关文章推荐
- 编译一个空类会默认生成哪些函数?
- 一个空类会生成哪些默认函数
- 一个空类会生成哪些默认函数
- C++空类默认生成哪些函数?
- 一个空类被编译器编译后产生了哪些默认函数
- 一个空类被编译器编译后产生了哪些默认函数
- 一个空类被编译器编译后产生了哪些默认函数
- C++中一个空类含有哪些默认的成员函数
- Effective C++ 第五 六条C++默认编写调用哪些函数,如何明确拒绝自动生成的函数
- 一个空类被编译器编译后产生了哪些默认函数
- 随机生成一个 10 个元素的数组,找到 x 的倍数,并将其值修改成 0.(注意:修改数值使用回掉函数处理)
- C++ 和 指针类 默认生成的四个函数
- C++中默认构造函数的生成(哪些情况会产生默认构造函数)
- 用分析函数实现自动生成sqlloader定长数据装载控制文件的一个语句
- debug_backtrace() 函数生成一个 backtrace(追踪)
- C++中的空类,默认产生哪些类成员函数
- C++ 中的空类,默认产生哪些类成员函数?
- 一个简单的PHP生成缩略图函数
- 定义一个空类时,C++到底默默编写了哪些函数?
- javascript 中的 prototype和constructor属性的使用(constructor属性始终指向创建当前对象的构造函数,每个函数都有一个默认的属性prototype,而这个prototype的constructor默认指向这个函数)