zoukankan      html  css  js  c++  java
  • 一个空类会生成哪些默认函数

    定义一个空类

    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

    ==

  • 相关阅读:
    DRP-ThreadLocal简单的理解
    Android开源项目SlidingMenu本学习笔记(两)
    [RxJS] Displaying Initial Data with StartWith
    [RxJS] Updating Data with Scan
    [RxJS] Stopping a Stream with TakeUntil
    [RxJS] Reactive Programming
    [RxJS] Reactive Programming
    [RxJS] Reactive Programming
    [RxJS] Starting a Stream with SwitchMap & switchMapTo
    [RxJS] Reactive Programming
  • 原文地址:https://www.cnblogs.com/graphics/p/1776950.html
Copyright © 2011-2022 走看看