Does our Student class have a destructor?
Student, its destructor needs to be invoked somewhere in this program. What does Student::~Student need to do? Does Student own any resources?
Student has no resources, so nothing special needs to be done.std::string members! Their destructors must be called, otherwise the memory is leaked!To define the destructor of Student: Just write an empty function body, and everything is done.
class Student {
std::string m_name;
std::string m_id;
int m_entranceYear;
public:
~Student() {}
};
class Student {
std::string m_name;
std::string m_id;
int m_entranceYear;
public:
~Student() {}
};
~Student() { std::cout << m_name << '\n'; }
Student(const std::string &name)
: m_name(name) /* ... */ {
// ...
}
~Student() {
// ...
}
For most cases, a class needs a destructor.
Therefore, the compiler always generates one
public by default.{}.We can explicitly require one by writing = default;, just as for other copy control members.
Type alias members
ClassName::AliasName.static members
static data members are like global variables, but in the class's scope.static member functions are like normal non-member functions, but in the class's scope. There is no this pointer in a static member function.static member belongs to the class, instead of any individual object.friend
friend declaration allows a function or class to access private (and protected) members of another class.friend is not a member.Definitions and declarations
Destructors
new/new[] are not required to destroyed. A delete/delete[] expression will destroy it, but it is not mandatory. So you can still create an object with a deleted destructor (see new expression, but you can't delete it, which possibly leads to memory leak.
inline public member of its class.
If an implicitly-declared destructor is not deleted, it is implicitly-defined by the compiler when it is odr-used. In some very special cases the compiler may fail to define the destructor (e.g. due to a member whose destructor is inaccessible). In that case, the destructor is implicitly deleted.