Default Link to heading
if class defines constructor, compiler won’t generate default constructor.
#include <iostream>
using namespace std;
class child
{
public:
int x;
child(int x) { cout << x << endl; }
// child() = default;
};
int main()
{
child c;
return 0;
}
so, this will fail with compile error
default_delete.cc:24:11: error: no matching function for call to ‘child::child()’
24 | child c;
| ^
But if still want default constructor, we can define the default constructor same as we don’t have the custom constructor
#include <iostream>
using namespace std;
class child
{
public:
int x;
child(int x) { cout << x << endl; }
child() = default;
};
int main()
{
child c;
return 0;
}
delete Link to heading
Same as default provides default implementation, delete
removed the default implementation generated by compiler. for example, the deletes the copy constructor.
#include <iostream>
using namespace std;
class child
public:
int x;
child() = default;
child(const child &) = delete;
};
int main()
{
child c;
child c1(c); // error because copy constructor is deleted
return 0;
}