There are 6 special member functions for structs or classes. We are here for object oriented programming, so I will give examples using classes.
Special member functions:
| Special Member Function | Task |
|---|---|
| Default Constructor | Creates a default object |
| Copy Constructor | Creates a new copy of the object |
| Copy Assignment Operator | Copies to the existing object |
| Move Constructor (C++11) | Creates a new object and moves the resources to the object |
| Move Assignment Operator (C++11) | Moves the resources to the existing object |
| Destructor | Destroys the object |
These functions are implicitly declared when needed by compiler. Explicitly defining all special member functions is not always necessary. In modern C++, the Rule of Zero recommends relying on automatically generated special member functions whenever possible. However, we will implement them manually to understand object copying and resource management.
Orthodox Canonical Form says you should define following 4 special member functions:
class Sample
{
public:
// Default Constructor
Sample();
// Copy Constructor
Sample(const Sample& other);
// Copy Assignment Operator
Sample& operator=(const Sample& other);
// Destructor
~Sample();
};
Here are some points to clearify:
- We pass the source object as a
constreference for two reasons: to avoid creating an unnecessary copy of the source object and to prevent modifying it inside the copy constructor or copy assignment operator. - The reason why we return
Sample&on copy assignment operator overloading is simply to be able to this:a = b = c. - Copy assignment operator does not create a new object. Beginners often confuse copy construction with copy assignment, so pay close attention to the difference. You can use
thiskeyword inside of it.
Let's create an example.
Step 1
#include <string>
#include <iostream>
class Human
{
private:
std::string _name;
public:
Human() : _name("A default human name")
{
std::cout << "Default Constructor: Human \"" << _name << "\" is created." << std::endl;
}
};
int main()
{
Human a;
return 0;
}
Output
Default Constructor: Human "A default human name" is created.
Step 2
#include <string>
#include <iostream>
class Human
{
private:
std::string _name;
public:
Human() : _name("A default human name")
{
std::cout << "Default Constructor: Human \"" << _name << "\" is created." << std::endl;
}
~Human()
{
std::cout << "Destructor: Human \"" << _name << "\" is destroyed." << std::endl;
}
};
int main()
{
Human a;
return 0;
}
Output
Default Constructor: Human "A default human name" is created.
Destructor: Human "A default human name" is destroyed.
Step 3
#include <string>
#include <iostream>
class Human
{
private:
std::string _name;
public:
Human() : _name("A default human name")
{
std::cout << "Default Constructor: Human \"" << _name << "\" is created." << std::endl;
}
Human(const Human& other) : _name(other._name)
{
std::cout << "Copy Constructor: Human \"" << _name << "\" is created." << std::endl;
}
~Human()
{
std::cout << "Destructor: Human \"" << _name << "\" is destroyed." << std::endl;
}
};
int main()
{
Human a;
Human b(a);
return 0;
}
Output
Default Constructor: Human "A default human name" is created.
Copy Constructor: Human "A default human name" is created.
Destructor: Human "A default human name" is destroyed.
Destructor: Human "A default human name" is destroyed.
Here is what happend step by step:
-
Human a-> it created aHumanobject with default constructor in variablea. -
Human b(a)-> it created aHumanobject with copy constructor. It write values ofato the new created objectb. -
Human bobject is implicitly destroyed. Local objects with automatic storage duration are destroyed in reverse order of their construction. We lastly create objectb, so we first destroyed it. Think stack logic. -
Human aobject is implicitly destroyed.
Step 4
#include <string>
#include <iostream>
class Human
{
private:
std::string _name;
public:
Human() : _name("A default human name")
{
std::cout << "Default Constructor: Human \"" << _name << "\" is created." << std::endl;
}
Human(const Human& other) : _name(other._name)
{
std::cout << "Copy Constructor: Human \"" << _name << "\" is created." << std::endl;
}
Human& operator=(const Human& other)
{
std::cout << "Copy Assignment Operator: Human \"" << _name << "\" is changed." << std::endl;
if (this == &other)
return *this;
this->_name = other._name;
return *this;
}
~Human()
{
std::cout << "Destructor: Human \"" << _name << "\" is destroyed." << std::endl;
}
};
int main()
{
Human a;
Human b(a);
a = b;
return 0;
}
Output
Default Constructor: Human "A default human name" is created.
Copy Constructor: Human "A default human name" is created.
Copy Assignment Operator: Human "A default human name" is changed.
Destructor: Human "A default human name" is destroyed.
Destructor: Human "A default human name" is destroyed.
It's boring to have the same name for everyone. Let's add another constructor, then test the special member functions we expliticitly defined.
Step 5
#include <string>
#include <iostream>
class Human
{
private:
std::string _name;
public:
// Orthodox Canonical Form
Human() : _name("A default human name")
{
std::cout << "Default Constructor: Human \"" << _name << "\" is created." << std::endl;
}
Human(const Human& other) : _name(other._name)
{
std::cout << "Copy Constructor: Human \"" << _name << "\" is created." << std::endl;
}
Human& operator=(const Human& other)
{
std::cout << "Copy Assignment Operator: Human \"" << _name << "\" is changed." << std::endl;
if (this == &other)
return *this;
this->_name = other._name;
return *this;
}
~Human()
{
std::cout << "Destructor: Human \"" << _name << "\" is destroyed." << std::endl;
}
// Other constructor
Human(const std::string& name) : _name(name)
{
std::cout << "Constructor: Human \"" << _name << "\" is created." << std::endl;
}
};
int main()
{
Human bob("Bob");
Human random;
random = bob;
return 0;
}
Constructor: Human "Bob" is created.
Default Constructor: Human "A default human name" is created.
Copy Assignment Operator: Human "A default human name" is changed.
Destructor: Human "Bob" is destroyed.
Destructor: Human "Bob" is destroyed.
You learned what orthodox canonical form is, and why it is used. This topic will be more important when you encounter the shallow copy and deep copy problems.
Top comments (0)