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
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.
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.
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.
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.
Because you have const
members. They cannot be assigned to, so the compiler cannot supply an assignment operator.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With