Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use of deleted functions in c++

Tags:

c++

eclipse

Here is a class I've created:

class A{  
private:  
    // some private data members:  
    // 2 const integers  
    // 2 integers  
    // 2 const strings  
public:  
    // C'tor  
    // D'tor  
void f(const A&);  
}

In the constructing of every object of this class there are no (explicit) dynamic allocations, only "primitive" types assignments. (by no explicit dynamic allocations i mean other than how string class handles memory).

when i try this:

void f(const A& item){  
    // do some thing  
*this = item;  
    // do other stuff  
}

i get the following error: "use of deleted function 'A& A::operator=(const A&)' "

now i know that the compiler is supposed to provide me a default assignment operator, and my question is: why the compiler refers to it's default assignment operator as a deleted function? and how do i fix this without assigning all the data member functions manually?

Thanks a lot! Gal

like image 476
bergerg Avatar asked Jun 15 '13 14:06

bergerg


People also ask

What is the use of Delete function?

Use the DELETE function to erase the data contents of a specified field, value, or subvalue and its corresponding delimiter from a dynamic array. The DELETE function returns the contents of the dynamic array with the specified data removed without changing the actual value of the dynamic array.

What is delete function C++?

Delete is an operator that is used to destroy array and non-array(pointer) objects which are created by new expression. Delete can be used by either using Delete operator or Delete [ ] operator. New operator is used for dynamic memory allocation which puts variables on heap memory.

When should you delete copy constructor?

Copy constructor (and assignment) should be defined when ever the implicitly generated one violates any class invariant. It should be defined as deleted when it cannot be written in a way that wouldn't have undesirable or surprising behaviour.

What will happen when copy constructor makes equal to delete?

The copy constructor and copy-assignment operator are public but deleted. It is a compile-time error to define or call a deleted function. The intent is clear to anyone who understands =default and =delete . You don't have to understand the rules for automatic generation of special member functions.


1 Answers

Because you have const members. They cannot be assigned to, so the compiler cannot supply an assignment operator.

like image 67
Oliver Charlesworth Avatar answered Oct 14 '22 16:10

Oliver Charlesworth