Making your class Non-Copyable and Non-Assignable

I was working on removing Coverity defects. My product is written such that we do not do any copy and assignment of the C++ classes .
So Coverity starting giving errors of following kind.

CID 28711: Other violation (MISSING_ASSIGN) and CID 28711: Other violation (MISSING_COPY)

Solution to this problem was to add private copy constructor and private assignment operator.
But since I had more than 500 classes do edit. I wrote a two new classes NonCopyable and NonAssignable
and inherit all the problem classes from the same.

class NonCopyable 
{
protected:
    NonCopyable() {}
    ~NonCopyable() {}
private:  
    NonCopyable( const NonCopyable& );
};

and

class  NonAssignable
{
protected:
    NonAssignable() {}
    ~NonAssignable() {}
private:  
    const NonAssignable& operator=( const NonAssignable& );
};

If you want to make your class Non copyable inherit from NonCopyable and If you want to make your class Non assignable inherit from NonAssignable

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s