http://www.cnblogs.com/graphics/archive/2010/07/14/1776950.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