in C Plus Plus by (5.1k points)

Deleted and Defaulted Functions

1 Answer

0 votes
by (5.1k points)

A function in the form:

struct A

{

A()=default; //C++11

virtual ~A()=default; //C++11

};

is called a defaulted function. The =default; part instructs the compiler to generate the default implementation for the function. Defaulted functions have two advantages: They are more efficient than manual implementations, and they rid the programmer from the chore of defining those functions manually.

The opposite of a defaulted function is a deleted function:

int func()=delete;

Deleted functions are useful for preventing object copying, among the rest. Recall that C++ automatically declares a copy constructor and an assignment operator for classes. To disable copying, declare these two special member functions =delete:

struct NoCopy

{

NoCopy & operator =( const NoCopy & ) = delete;

NoCopy ( const NoCopy & ) = delete;

};

NoCopy a;

NoCopy b(a); //compilation error, copy ctor is deleted

Related questions

+1 vote
asked Jan 2, 2020 in C Plus Plus by GeorgeBell (5.1k points)
+1 vote
asked Jan 2, 2020 in C Plus Plus by GeorgeBell (5.1k points)
+1 vote
asked Jan 2, 2020 in C Plus Plus by GeorgeBell (5.1k points)
+1 vote
asked Jan 2, 2020 in C Plus Plus by GeorgeBell (5.1k points)
+1 vote
asked Jan 2, 2020 in C Plus Plus by GeorgeBell (5.1k points)
+1 vote
asked Jan 2, 2020 in C Plus Plus by GeorgeBell (5.1k points)
+1 vote
asked Jan 2, 2020 in C Plus Plus by GeorgeBell (5.1k points)
+1 vote
asked Jan 2, 2020 in C Plus Plus by GeorgeBell (5.1k points)
0 votes
asked Jan 2, 2020 by GeorgeBell (5.1k points)
+1 vote
asked Mar 17, 2020 in C Plus Plus by SakshiSharma (32.2k points)
+1 vote
+1 vote
asked Jan 4, 2020 in C Plus Plus by AdilsonLima (6.2k points)
+1 vote
+1 vote
asked Jan 4, 2020 in C Plus Plus by AdilsonLima (6.2k points)
+1 vote
asked Jul 16, 2019 in C Plus Plus by Indian (793 points)
...