Proficient classes and objects
This article is based on the knowledge of classes and objects that you already know.
Dynamic Memory Allocation in the object:
If the memory is dynamically allocated to the object, the memory should be released in the destructor. Therefore, you need to write your own destructor.
Process replication and assignment:
C ++ will generate a copy constructor or value assignment operator without writing the copy constructor and value assignment operator. The methods generated by these compilers call the copy constructor or value assignment operator on the object member recursively. However, for basic types (such as int, double, and pointer), the shortest or bitwise replication (Value assignment) is provided: Only data members are directly copied from the source object or assigned to the target object. If the memory is dynamically allocated to the object, the problem may occur.
To solve the above problem: as long as the memory is dynamically allocated in the class, you should write your own copy constructor to provide deep memory replication. In the copy constructor, you must copy all data members, not just pointer members.
When assigning values to an object dynamically allocated with memory, you need to write the value assignment operator yourself because it has been initialized. before allocating new memory, the allocated memory needs to be released.
Sometimes, when the memory is dynamically allocated in the class, the simplest way is to prohibit others from copying or assigning values to your objects. To do this, you can mark the replication constructor or operator = as private, in this case, you do not have to implement the two operations.
To sum up, as long as the memory needs to be dynamically allocated, you need to write your own copy constructor, destructor, and value assignment operators.
Different types of data members:
Static data member: a data member associated with a class rather than an object. It belongs to a class rather than an object.
Const data member: once created and initialized, this data member cannot be changed.
When constants are only used for classes, static const data members should be used instead of global constants. And these constants must be declared in the source file, which is also the last chance to assign values to them:
Class SpreadsheetCell
{
... Code...
Public:
Static const int kMaxHeight;
... Code...
};
.... Cpp...
Const int SpreadsheetCell: kMaxHeight = 100;
If statict const member variables are simple types, the C ++ standard allows declaring these variables in the class definition file and assigning values to them at the same time.
Reference data member: If a data member needs to be referenced in the class definition, and the member type is another class, you must understand the referenced class instead of using a # include, we can use the advanced reference of the class.
Class SpreadsheetApplication; // advanced Declaration
Class SpreadsheetCell
{
... Code ....
SpreadsheetApplication & app;
};
At the same time, the initialization list of the SpreadsheetCell constructor must be app initialization. This is also true in the replication constructor. But note: Once initialized, you cannot modify the object it references. Therefore, you do not need to try to assign a value to this reference in the value assignment operator.
Const reference data member: a regular reference can point to a const object. Similarly, a reference member can also point to the const object const SpreadsheetApplication & app;
Static reference members can also be referenced, but static const is rarely used.
In-depth understanding:
Static Method: This method is applied to the entire class, not just an object. To implement this function outside the class, you do not need to add the keyword static. It is not called on a specific object, so it does not have this pointer and cannot be executed on a specific object to access its non-static members. In fact, static methods are equivalent to regular functions. The only difference is that it can be a private or protected static data member of the hosts class.
Remember: do not access non-static members in static methods.
When calling, the scope parsing operator is required.
Const method: a const object is an object whose values cannot be changed. If a const object or a const object is applied, the compiler will not allow you to call any method on the object, unless you can ensure that the called method will not modify any data members. To ensure that a method does not modify any data member, the method itself is marked with the const keyword. For example, a member function of the class: string getString () const; if this method is defined outside the class, the const keyword must also be added.
If a method is declared as const, but it does modify the data member, the compiler reports an error. Static methods cannot be declared as const because they are redundant and there is no corresponding instance for static methods, so they cannot modify internal values. If each data member in a method has a const reference, this method is useful if its value is const. Once modified, an error is returned.
Non-const objects can call the const and non-const methods, but the const object can only call the const method.
You should develop a habit of declaring all methods that do not modify the object as the const method, so that you can use the reference of the const object in the program.
Note that the const object can also be undone and Its destructor can be called. Do not mark the Destructor as const.
If you want to modify a data member in the const method, you can add the mutable keyword before the data member in the class definition. For example, mutable int value;
Method overload:
C ++ does not allow a method name to be reloaded only based on the returned type, because in many cases, the compiler cannot determine which version of the method to call.
However, you can give the const to reload a method. That is to say, you can write two methods with the same name and the same parameters. One declaration is const, and the other is not. If you provide a const object, the const method is called. If you provide a non-const object, the non-const method is called.
Default parameters:
C ++ provides a feature similar to method overloading, called a default parameter. You can specify the default value for the function and method parameters in the prototype. If a real parameter is specified, the default value is ignored. If no real parameter is specified, the default value is used. Default parameters are the most useful in constructors.
Note: Only one consecutive parameter table starting with the rightmost parameter can provide default parameters. Otherwise, the compiler will not be able to match the default value of the provided instances.
Note: The default parameter is only specified in the method declaration and is not specified in the definition. If you want to declare a default constructor and a multi-parameter default constructor at the same time and all its parameters have default values, the compiler reports an error. If no parameter is specified, it does not know which constructor to call.
Inline method:
In C ++, you can suggest that a method or function call should not be used as a method or function call. In fact, the compiler should insert this method or function body directly into the position of the method or function call in the code. This process is called inline. Therefore, methods or functions must be called inline methods or inline functions. This process is only a safer version of the # define macro.
In a function or method definition, you can specify an inline method and function by placing the keyword inline before it.
Note: inline methods and function definitions should be provided in all source files that require calling inline methods or functions. Therefore, if you have compiled inline functions or methods, you should put their definitions on the prototype of the same inline function or method in the header file. For methods, it is safe to put the method definition in the. h file that includes the class definition. This is like a # define macro.
In C ++, another syntax is provided to declare the inline method, where the inline keyword is not used at all. The specific method is to put the method definition directly in the class definition.
It should be mentioned that, first, there are many restrictions on whether a method can become an inline method. The compiler will only inline the simplest method or function. If an inline method or function is defined, but the compiler does not want to inline this method or function, inline commands may be ignored without any prompts. Second, inline may result in code expansion. Therefore, use inline with caution.
Youyuan function:
C ++ allows classes to declare other classes or non-member functions as friend functions, and these friends can access protected and private data members and methods. You do not need to add the friend keyword before the function definition.
Operator overload:
See C ++ learning 4. Reload the C ++ operator.
From my dream pursued by me