0

I have the following piece of the code:

template<typename T>
class derClass : public baseClass<column<T>, column<k>>
{
      //prohibit value semantics
      derClass(const derClass&) = delete;
      derClass& operator= (const derClass&) = delete;

   public:
      ...
}

There are many places of this code that I do not understand:

  1. What does these delete mean? I do not see any declaration of the delete variable.
  2. Why do we need a constructor that takes as argument an object of the same class?
  3. What does this whole line mean: derClass& operator= (const derClass&) = delete;
Brian Tompsett - 汤莱恩
  • 5,438
  • 68
  • 55
  • 126
Roman
  • 112,185
  • 158
  • 335
  • 439

4 Answers4

4

The delete here is a new C++11 thing, and tells the compiler that these methods doesn't exist. Otherwise the compiler would have created default versions of them.

The constructor taking a constant reference of its own class is a copy constructor. What the copy constructor is for should be explained in any beginners text about C++.

As the copy constructor and copy assignment operator are now deleted, it means you can't create an instance of derClass from another instance of derClass.

Some programmer dude
  • 380,411
  • 33
  • 383
  • 585
1
  1. It is C++11 syntax that essentially means "this class does not have an X", where X is, first, a copy constructor, and second, a copy assignment operator.
  2. It is a copy constructor. This is explained in any introductory C++ book.
  3. It is a copy assignment operator. This is explained in any introductory C++ book.

Overall, the use of these deleted special functions means you cannot copy instances of that class, or copy assign to them:

derClass d1, d2;
derclass d3{d1}; // ERROR: cannot copy
d2 = d1;         // ERROR: cannot copy-assign
juanchopanza
  • 216,937
  • 30
  • 383
  • 461
1

The thing you are seeing is used to disable copy constructor.

For more info see: What's the most reliable way to prohibit a copy constructor in C++?

Community
  • 1
  • 1
1

Value semantics is when your object behaves like value: when you assign it to another object second object will have the same value, when you chnge first the second is left the same. The other case is reference semantics - when you change first - the second changes.

The constructor, taking the same class's reference is copy-constructor, it just copies object. The second definition is assignment operator. When you mark them as delete - your object could not be copied and assigned.

kassak
  • 3,826
  • 24
  • 36