If you do not define these four methods the compiler will generate them for you:
If you define a class like this:
struct some_struct: public some_base
{
std::string str;
int a;
int b;
int c;
}
What the compiler will build is:
struct some_struct: public some_base
{
std::string str;
int a;
int b;
int c;
some_struct()
:some_base()
// PODS not initialised
,str()
{}
some_struct(some_struct const& copy)
:some_base(copy)
,str(copy.str)
,a(copy.a)
,b(copy.b)
,c(copy.c)
{}
some_struct& operator=(some_struct const& copy)
{
some_base::operator=(copy);
str = copy.str;
a = copy.a;
b = copy.b;
c = copy.c;
return *this;
}
~some_struct()
{}
// Note the below is psedo code
// Also note member destuction happens after user code.
// In the compiler generated version the user code is empty
// PODs dont have destructor
:~str()
,~some_base();
// End of destructor here.
}
If you want to know why?
It is to maintain backward compatibility with C. But it also makes writting simple classes easier. Some would argue that it adds problems because of the "shallow copy problem". My argument against that is that you should not have a class with owned RAW pointers in it. By using the appropriate smart pointers that problem goes away.
Default Constructor (If no othere constructors are defined)
The compiler generated default constructor will call the base classes default contructor and then each memebers default constructor (in the order they are declared)
Destructor (If no destructor defined)
Calls the destructor of each member in reverse order of declaration. Then calls the destructor of the base class.
Copy Constructor (If no copy constructor is defined)
Calls the base class copy constructor passing the src object. Then calls the copy constructor of each member using the src objects members as the value to be copied.
Assignment Operator
Calls the base class assingment operator passsing the src object. Then calls the assignment operator on each member using the src object as the value to be copied.