I have finished C ++, but I didn't seem to have learned anything this semester. I think C ++ is not getting started yet. I want to study C ++ again by taking notes, here I will write my personal thoughts on C ++ and what I don't understand here (I hope the experts can help me solve what I don't understand and point out what I am wrong, thank you ), I wrote the first article today. I should write one article every day.
I. const, enum, and inline usage summary:
When replacing the # define parameter without parameters, use const and enum not only because it is more secure than # define (type check will be performed ), # The macros defined by define do not have parameter types, but are simply replaced, in addition, const can be used for objects in any scope, function parameters, return value types of functions, and member function bodies (which can prevent user modification ). if you have a parameter, use inline instead of a macro with the parameter (to avoid ambiguity and some errors ). the variable modified by const can be expressed in one sentence as "the first month of the near-water building (the data type of the variable is not checked )"
What's interesting is that since you said this, I will do this.
Eg0:
Const char * str = "Name"; // If char is not viewed, const modifies * str (the pointer to a constant is that * str content cannot be changed, but its address can be changed.
Str [0] = 'F' // false
Str = "Fame" // true
And:
Char * const str = "Name" // If char is not viewed, const modifies str (constant pointer). The content can be changed, but the address cannot be changed.
Char * const str = "Name"; // constant pointer
Str [0] = 'F'; // No syntax error is returned, but data conflict occurs.
// Unhandled exception at 0x00d914d8 in Test.exe: 0xC0000005: Access violation writing location 0x00d97838. What is the cause ???
But I use new to assign five spaces to str and assign values one by one. Why ???
Char * const str = new char [5];
Str [0] = 'F ';
Str [1] = 'a ';
Str [2] = 'M ';
Str [3] = 'E ';
Str [4] = '\ 0 ';
At this time, str output is: Fame (please tell me the reason, thank you)
Const is better than # define
Eg1:
# Define MAX 5 // available below
Const int cmax = 5; // Replace (the compiler checks the type during compilation)
If the initial value of the type cannot be defined in the Declaration of class, but the array size needs to be determined, it can be replaced by enum or static const # define.
Eg2:
Class CGamePlayers
{
Private:
Enum {NumTurns = 5 };
Int m_Scores [NumTurns];
};
Or
Class CGamePlayers
{
Private:
Static const int NumTurns = 5; // declare the variable NumTurns. Note that static cannot be less, because the variables declared by static are in the compiled
// Determine the size of the statement. Otherwise, the following statement will fail.
Int m_Scores [NumTurns];
};
Const int CGamePlayers: NumTurns; // Its size cannot be defined, because the NumTurn size cannot be changed to 5.
It is highly efficient to use inline instead of macro with parameters.
Eg3:
# Define CALL_WITH_MAX (lhs, rhs) Fn (lhs)> (rhs )? (Lhs): (rhs) // assume that the Fn function has been defined. Remember to include "()"
Int lhs = 5, rhs = 0;
CALL_WITH_MAX (++ lhs, rhs); // a is accumulated twice to output lhs = 7
CALL_WITH_MAX (++ lhs, rhs + 10); // a is accumulated once and output lhs = 6
Believe it or not, give it a try !! Why does the lhs automatically increase twice when the first parameter is greater than the second parameter, while the lhs does not, I didn't understand (I thought that when lhs> rhs, he would call the Fn function twice. In fact, both statements are called once, but why is the increase in the number of lhs affected by the size of rhs I have not understood (Thank you for your advice)
If inline is used, such ambiguous statements do not exist.
Template <typename T>
Inline void Call_With_Max (const T & lhs, const T & rhs)
{
Fn (lhs> rhs? Lhs: rhs); // assume that the Fn function has been defined
}
The value of lhs on the other side of the function is 6 twice (in line with our normal thinking ).
From Cql_liliang's Blog