课程目标
- 以良好的方式编写C++ class
(Object Based: 面对的是单一class的设计)- class without pointer members -- Complex (week 1)
- class with pointer members -- String (week 2)
- 学习Classes之间的关系
(Object Oriented: 面对的是多重classes的设计, classes和classes之间的关系)- 继承 (inheritance)
- 复合 (composition)
- 委托 (delegation)
Big Three, 三个特殊函数
class String {
public:
String(const char* cstr = 0);
String(const String &str); // 1. copy constructor
String& operator=(const String &str); // 2. copy assignment
~String(); // 3. destructor
char* get_c_str() const { return m_data; }
private:
char *m_data;
};
关于copy control (Big Three + move constructor / assignment) (摘自C++ Primer):
When we define a class, we specify—explicitly or implicitly—what happens when objects of that class type are copied, moved, assigned, and destroyed. A class controls these operations by defining five special member functions: copy constructor, copy-assignment operator, move constructor, move-assignment operator, and destructor. The copy and move constructors define what happens when an object is initialized from another object of the same type. The copy- and move-assignment operators define what happens when we assign an object of a class type to another object of that same class type. The destructor defines what happens when an object of the type ceases to exist. Collectively, we’ll refer to these operations as copy control.
Constructor 和 Destructor (构造函数和析构函数)
inline
String::String(const char *cstr = 0)
{
if (cstr) {
m_data = new char [strlen(cstr) + 1];
strcpy(m_data, cstr);
} else {
m_data = new char [1];
*m_data = '\0';
}
}
inline
String::~String()
{
delete[] m_data;
}
析构函数的定义(摘自C++ Primer):
The destructor operates inversely to the constructors: Constructors initialize the nonstatic data members of an object and may do other work; destructors do whatever work is needed to free the resources used by an object and destroy the
nonstatic data members of the object.
The destructor is a member function with the name of the class prefixed by a tilde (~). It has no return value and takes no parameters. Because it takes no parameters, it cannot be overloaded. There is always only one destructor for a given class.
析构函数 何时被调用? (摘自C++ Primer):
The destructor is used automatically whenever an object of its type is destroyed:
- Variables are destroyed when they go out of scope.
- Members of an object are destroyed when the object of which they are a part is destroyed.
- Elements in a container—whether a library container or an array—are destroyed when the container is destroyed.
- Dynamically allocated objects are destroyed when the delete operator is applied to a pointer to the object.
- Temporary objects are destroyed at the end of the full expression in which the temporary was created.
Because destructors are run automatically, our programs can allocate resources and (usually) not worry about when those resources are released.
Copy Constructor (拷贝构造函数)
inline
String::String(const String &str) // deep copy
{
m_data = new char [strlen(str.m_data) + 1];
strcpy(m_data, str.m_data);
}
拷贝构造函数的定义 (摘自C++ Primer):
A constructor is the copy constructor if its first parameter is a reference to the class type and any additional parameters have default values
拷贝构造函数 何时被调用? (摘自C++ Primer):
Copy initialization ordinarily uses the copy constructor (or the move constructor), and it happens when we
- Define variables using an =;
- Pass an object as an argument to a parameter of nonreference type;
- Return an object from a function that has a nonreference return type;
- Brace initialize the elements in an array or the members of an aggregate class;
- Some class types also use copy initialization for the objects they allocate.
Copy Assignment Operator (拷贝赋值函数)
inline
String& String::operator=(const String &str)
{
if (this == &str) // self assignment
return *this;
delete [] m_data;
m_data = new char [strlen(str.m_data) + 1];
strcpy(m_data, str.m_data);
return *this;
}
关于 operator overloading 和 copy assignment (摘自C++ Primer):
Overloaded operators are functions that have the name operator followed by the symbol for the operator being defined. Hence, the assignment operator is a function named operator=. Like any other function, an operator function has a return type and a parameter list.
The parameters in an overloaded operator represent the operands of the operator. Some operators, assignment among them, must be defined as member functions. When an operator is a member function, the left-hand operand is bound to the implicit this parameter. The right-hand operand in a binary operator, such as assignment, is passed as an explicit parameter.
The copy-assignment operator takes an argument of the same type as the class. To be consistent with assignment for the built-in types, assignment operators usually return a reference to their left-hand operand. It is also worth noting that the library generally requires that types stored in a container have assignment operators that return a reference to the left-hand operand.
何时定义 Big Three (The Rule of Three)
规则一: Classes That Need Destructors Need Copy and Assignment (摘自 C++ Primer):
One rule of thumb to use when you decide whether a class needs to define its own versions of the copy-control members is to decide first whether the class needs a destructor. Often, the need for a destructor is more obvious than the need for the copy constructor or assignment operator. If the class needs a destructor, it almost surely needs a copy constructor and copy-assignment operator as well.
规则二: Classes That Need Copy Need Assignment, and Vice Versa (摘自 C++ Primer):
Although many classes need to define all of (or none of) the copy-control members, some classes have work that needs to be done to copy or assign objects but has no need for the destructor. As an example, consider a class that gives each object its own, unique serial number. Such a class would need a copy constructor to generate a new, distinct serial number for the object being created. That constructor would copy all the other data members from the given object. This class would also need its own copy-assignment operator to avoid assigning to the serial number of the left-hand object. However, this class would have no need for a destructor.
Stack 和 Heap (栈和堆)
摘自C++ Primer:
Stack memory is used for nonstatic objects defined inside functions. Objects allocated in static or stack memory are
automatically created and destroyed by the compiler. Stack objects exist only while the block in which they are defined is executing; static objects are allocated before they are used, and they are destroyed when the program ends.
In addition to static or stack memory, every program also has a pool of memory that it can use. This memory is referred to as the free store or heap. Programs use the heap for objects that they dynamically allocate—that is, for objects that the program allocates at run time. The program controls the lifetime of dynamic objects; our code must explicitly destroy such objects when they are no longer needed.