uncopyable
Overview
estd::uncopyable
is a utility that can be used to make a class uncopyable.
Contains uncopyable class and macro. The macro can be used to make a class
uncopyable that cannot inherit from the uncopyable base class.
Usage
The usage guideline for uncopyable is provided below:
Usage guidance |
A class can be made uncopyable using two ways either by using the macro or by inheriting from the uncopyable class. The class should be made uncopyable if it is not intended to be copied or assigned to. |
Example
The following example shows how to make a class uncopyable using macro:
class MyClass
{
UNCOPYABLE(MyClass);
public:
MyClass(int n) : n(n) {}
private:
int n;
};
void example_uncopyable_macro()
{
MyClass obj(10);
// Usage of copy construction calls will result in compilation error.
// MyClass obj1 = obj;
// MyClass obj2(obj);
}
The below code shows how to make a class uncopyable by inheriting from uncopyable:
class MyClass1 : public ::estd::uncopyable
{
public:
MyClass1(int n) : n(n) {}
private:
int n;
};
void example_uncopyable_inheritance()
{
MyClass1 obj(10);
// Usage of copy construction calls will result in compilation error.
// MyClass1 obj1 = obj;
// MyClass1 obj2(obj);
}