Questions tagged [deleted-functions]

C++11 introduced the ability to mark member functions as deleted, which means that any attempt to call those functions causes a compilation error. This can be used to prevent improper usage of a class. For example, if a class is meant to manage a unique resource, an object of that class shouldn't be copiable. This can be achieved by deleting its copy constructor and copy assignment functions.

With C++11 the delete keyword can be used to explicitly forbid the usage of a member function: if it is deleted, any attempt to call it will cause a compilation error (there is no difference at runtime). This is useful when calling that function could lead to wrong results and/or unexpected behaviour.

An example is a class that has to manage a unique resource: an object of that class must not be copiable. Normally, to ensure that a function is not used, a programmer must simply avoid declaring it. But in some cases it is not enough, because some functions can be automatically generated by the compiler even though the programmer hasn't declared them. These functions include constructors, copy/move constructors and copy/move assignments. Not defining them doesn't guarantee that they can't be used. The solution is to use the = delete; syntax.

For example, the following class declares a constructor and a destructor, but deletes the copy constructor and copy assignment:

class Unique_manager {
    Unique_manager();                                          // Constructor
    ~Unique_manager();                                         // Destructor
    Unique_manager(const Unique_manager&)            = delete; // Forbid copy constructor
    Unique_manager& operator=(const Unique_manager&) = delete; // Forbid copy assignment
};

In this case it would be possible to create an object of this class:

Unique_manager db_access_manager;

but an attempt to create a copy from it would be blocked by the compiler:

Unique_manager another_db_manager(db_access_manager);  // Error: copy constructor
Unique_manager one_more_access_manager;                // OK:    constructor
one_more_access_manager = db_access_manager;           // Error: copy assignment
104 questions
0
votes
2 answers

Resolving a compiler error due to an invariant member with a possible deleted default constructor

I have asked a series of questions that all relate to the same source code in this…
Francis Cugler
  • 7,788
  • 2
  • 28
  • 59
0
votes
2 answers

Force compiler to emit error on move constructor not defined with a base class deleted copy ctor

Consider a base class which prevents copy construction and copy assignment like this: class NonCopyable { public: NonCopyable() = default; ~NonCopyable() = default; NonCopyable(NonCopyable const&) =…
Ayak973
  • 468
  • 1
  • 6
  • 23
0
votes
1 answer

C++ Poco - How to create a vector of NotificationQueue's?

I want to create a Notification Center, where I handle all the notifications to threads. I can't tell on the software boot how many notification queues I would need. It may vary during run-time. So I've created this (code simplified): #include…
waas1919
  • 2,365
  • 7
  • 44
  • 76
0
votes
0 answers

Error 'CDC::CDC(const CDC &)': attempting to reference a deleted function MFCBreakout

Making my Breakout Game in MFC. I store the bitmap info into a CDC object (Device Context Object), but since there will be different bitmaps for each block, I store the same overwritten CDC m_blockDC into a std::vector m_blockStates and since I…
0
votes
1 answer

Enforcing move only semantics

I am relatively new to C++11, though I have used previous versions for many years. Is this the correct way to enforce that an object will only be movable? class CResource { public: CResource(); CResource(CResource &&); CResource &…
0
votes
2 answers

Attempting to reference a deleted function error when swapping priority queues

I was just trying to swap the data between the two priority queues and got this error. I also did some googling and still don't know what's wrong here. #include class Node { public: int idx; }; auto greater = []( const Node& a, const…
Stoatman
  • 758
  • 3
  • 9
  • 22
0
votes
1 answer

VFP. Deleted records – indexing - re-creation of records

I have a table of sales order lines (sDetail); There is an index on the records which is effectively a candidate index with a key of the Order reference plus STR( line number). This is used for retrieving the lines of a particular order. There…
Andrew_46
  • 37
  • 2
  • 13
0
votes
1 answer

Macro to make class noncopyable

Is there any problem with following macro that makes a class non-copyable? #define PREVENT_COPY(class_name) \ class_name(const class_name&) = delete;\ class_name& operator=(const class_name&) = delete; class Foo { public: PREVENT_COPY(Foo) …
quantum_well
  • 869
  • 1
  • 9
  • 17
-1
votes
2 answers

Is it possible to free dynamic objects created via new using free?

I know that C++ allows creating a class with a deleted or inaccessible destructor: struct Foo{ Foo() = default; ~Foo() = delete; }; Foo f;// error: ~Foo is deleted Foo* pf = new Foo();// ok Now it's OK to create a dynamic object but it…
Itachi Uchiwa
  • 3,044
  • 12
  • 26
-1
votes
1 answer

how to return the result of addition of two objects of a class

On compiling it is showing sme error like use of delete function constexpr Player::Player(const Player&) when it is return the result of addition of the objects. #include using namespace std; class Player { char* name; int…
-1
votes
1 answer

Equal operator attempting to reference a deleted function, array

I am using SFML to create a space invaders clone with C++. I am quite new to C++ and I'm learning it as I'm working on this project. I am getting the following error when trying to create a new enemy using the constructor and array to set settings…
firepro20
  • 63
  • 1
  • 8
-1
votes
2 answers

C++ Error (C2280) tring to access a deleted function

So, i was trying to make a 2d game with opengl and sfml, so i created a button class in an input namespace, i made a render() function in it, but when i call it (no matter wheter i use a pointer or i don't) even if I pass all the required arguments…
Chappie733
  • 45
  • 6
-1
votes
1 answer

C++ Thread in an unordered_map (no copy constructor)

I am trying to figure out a way to get a thread out of an unordered_map in c++. However, I am getting the std::thread::thread(const std::thread &) attempting to reference a deleted function. Example: #include "stdafx.h" #include…
Questionable
  • 768
  • 5
  • 26
-2
votes
1 answer

Finding where a deleted function is referenced

IDE - Visual Studio Express 2013 for Desktop C++11 Problem - I have a class which is apparently being copied (using copy constructor). If I declare the copy constructor like this: MyClass(const MyClass&) = delete; It complains about a reference to…
brettwhiteman
  • 4,210
  • 2
  • 29
  • 38
1 2 3 4 5 6
7