C++ Tip: Make Your Class Non-Copyable Without Boost

Introduction

There are times where an object should never be passed by copy but by reference or pointer. For instance, the class has a data member (like counter or mutex) which should not be duplicated. In this tip, we take a look at 2 techniques which declare the class to be non-copyable without resorting to using Boost’s Noncopyable class.

Use delete Keyword

Delete the copy constructor and assignment operator. This works for C++11 and above.

// Works for C++11 and above
class DeletedCopyFunc
{
public:
    DeletedCopyFunc(int value): m_Value(value) {}
public:
    DeletedCopyFunc(const DeletedCopyFunc&) = delete;
    DeletedCopyFunc& operator=(const DeletedCopyFunc&) = delete;

private:
    int m_Value;
    std::mutex m_Mutex;
};

Make private

Declaring the copy constructor and assignment operator private is another way and it is perfectly fine not to define their bodies. This technique works for all C++ versions.

// Works for all C++ versions
class PrivateCopyFunc
{
public:
    PrivateCopyFunc(int value) : m_Value(value) {}
private:
    PrivateCopyFunc(const PrivateCopyFunc&);
    PrivateCopyFunc& operator=(const PrivateCopyFunc&);

private:
    int m_Value;
    std::mutex m_Mutex;
};

How Boost Does It?

It can be seen from the Boost noncopyable source that it also uses the same techniques.

class noncopyable
  {
  protected:
#if !defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS) && 
         !defined(BOOST_NO_CXX11_NON_PUBLIC_DEFAULTED_FUNCTIONS)
      BOOST_CONSTEXPR noncopyable() = default;
      ~noncopyable() = default;
#else
      noncopyable() {}
      ~noncopyable() {}
#endif
#if !defined(BOOST_NO_CXX11_DELETED_FUNCTIONS)
      noncopyable( const noncopyable& ) = delete;
      noncopyable& operator=( const noncopyable& ) = delete;
#else
  private:  // emphasize the following members are private
      noncopyable( const noncopyable& );
      noncopyable& operator=( const noncopyable& );
#endif
  };

Bonus: When copy Constructor and Assignment Operator are Called

Having done interviews over the years, I discover to my dismay that many job candidates are not aware of when copy constructor and assignment operator are called. Run the code below to see which lines are printed.

class CopyableClass
{
public:
    CopyableClass(int value) : m_Value(value) {}
    CopyableClass(const CopyableClass& that)
    {
        std::cout << "CopyableClass Copy Constructor called!" <m_Value = that.m_Value;
    }
    CopyableClass& operator=(const CopyableClass& that)
    {
        std::cout << "CopyableClass Assignment Operator called!" <m_Value = that.m_Value;
        return *this;
    }

private:
    int m_Value;
};

int main()
{
    CopyableClass a(10);
    CopyableClass b = a; // CopyableClass Copy Constructor called!
    b = a; // CopyableClass Assignment Operator called!

    CopyableClass c(a); // CopyableClass Copy Constructor called!

    return 0;
}

The example code is hosted at Github.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this:
search previous next tag category expand menu location phone mail time cart zoom edit close