LoginSignup
0
0

More than 5 years have passed since last update.

[C++11] Explicitly delete functions

Posted at

In the C++11, there is a new feature to explicitly delete functions. With this new feature, for example we can delete the copy constructor and copy-assignment operator that compiler automatically generates for us to prevent copy operation.

Before C++11

class Uncopyable {
public:
    Uncopyable() {};
private:
    Uncopyable(const Uncopyable& oth);
    Uncopyable& operator=(const Uncopyable& oth);
};

Before C++11, this code is a solution to prevent copy operation. But it still be copyable for a friend class of Uncopyable.

In C++11

class Uncopyable {
public:
    Uncopyable() {};

    Uncopyable(const Uncopyable& oth) =delete;
    Uncopyable& operator=(const Uncopyable& oth) =delete;
};

In C++11, there is a more straightforward way to implement this. The =delete marks the function to be deleted. When you do something like this

Uncopyable a, b;
b = a;

It will give you a error message at compile time.

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0