In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "what is the concept of Const,Const function and Const variable in C++". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
When you see the const keyword, the first thing C++ programmers think of is probably the const constant. This is not a good reflex. If you only know how to use const to define constants, it is equivalent to using gunpowder only to make firecrackers. The greater charm of const is that it can modify the parameters of a function, return values, and even the definition body of a function.
Const is the abbreviation of constant, which means "constant". Everything decorated by const is forcefully protected against accidental changes and improves the robustness of the program. So many C++ programming books suggest: "Use const whenever you need".
1. Modify the parameters of a function with const
If the parameter is used for output, no matter what data type it is, no matter it uses "pointer passing" or "reference passing", it cannot be modified by const, otherwise the parameter will lose its output function. Const can only modify input parameters:
If the input parameter is "pointer passing", then const modification can prevent accidental changes to the pointer, which plays a protective role.
For example, the StringCopy function:
Void StringCopy (char * strDestination, const char * strSource)
Where strSource is the input parameter and strDestination is the output parameter. After const is added to the strSource, if the statement in the body of the function tries to change the contents of the strSource, the compiler will point out the error.
If the input parameter uses "value passing", because the function will automatically generate a temporary variable to copy the parameter, the input parameter does not need to be protected, so do not add const decoration.
For example, do not write the function void Func1 (int x) as void Func1 (const int x). Similarly, do not write the function void Func2 (An a) as void Func2 (const An a). Where An is a user-defined data type.
For parameters of non-internal data types, functions such as void Func (An a) are bound to be less efficient. Because the function body will produce a type A temporary object to copy the parameter a, and the temporary object construction, copying, and destructing process will take time.
To improve efficiency, you can change the function declaration to void Func (A & a), because "reference passing" only borrows the alias of the parameter and does not need to produce temporary objects. But the function void Func (A & a) has one drawback:
"reference passing" may change the parameter a, which we do not expect. It's easy to solve this problem, just decorate it with const, so the function eventually becomes void Func (const A & a).
And so on, should void Func (int x) be rewritten to void Func (const int & x) to improve efficiency? It is not necessary at all, because there is no process of constructing and destructing parameters of internal data types, and replication is very fast, and the efficiency of "value passing" and "reference passing" is almost equal.
The problem is so lingering that I have to summarize the use of the "const &" modifier input parameter.
For input parameters of non-internal data types, the way of "value passing" should be changed to "const reference passing" in order to improve efficiency. For example, change void Func (An a) to void Func (const A & a).
For input parameters of internal data types, do not change the way of "value passing" to "const reference passing". Otherwise, it can not only improve the efficiency, but also reduce the understandability of the function. For example, void Func (int x) should not be changed to void Func (const int & x).
2 modify the return value of the function with const
If the return value of a function in the way of "pointer passing" is modified with const, then the content of the function return value (that is, the pointer) cannot be modified, and the return value can only be assigned to a pointer of the same type with const modification. For example, a function
Const char * GetString (void)
A compilation error will occur in the following statement:
Char * str = GetString ()
The correct usage is
Const char * str = GetString ()
If the return value of the function adopts the "value passing mode", it is worthless to add const decoration because the function will copy the return value to the external temporary storage unit.
For example, do not write the function int GetInt (void) as const int GetInt (void).
Similarly, do not write the function A GetA (void) as const A GetA (void), where An is a user-defined data type.
If the return value is not an internal data type, rewriting the function A GetA (void) to const A & GetA (void) does improve efficiency. But be careful at this time, be sure to find out whether the function wants to return a "copy" of an object or just an "alias", otherwise the program will make an error.
It is rare for the return value of a function to be passed by reference, which generally appears only in the assignment function of the class in order to achieve chained expression.
For example:
Class A
{
A & operate = (const A & other); / / assignment function
}
An a, b, c; / a, b, c are the objects of A.
A = b = c; / / normal chain assignment
(a = b) = c; / / abnormal chain assignment, but legal
If the return value of the assignment function is modified with const, the content of the return value is not allowed to be changed. In the above example, the statement a = b = c is still correct, but the statement (a = b) = c is illegal.
3 const member function
Any function that does not modify the data member should be declared as a const type. If you accidentally modify the data member or call other non-const member functions when writing const member functions, the compiler will point out the error, which will undoubtedly improve the robustness of the program. In the following program, the member function GetCount of class stack is only used for counting, and logically GetCount should be a const function. The compiler will point out the error in the GetCount function.
Class Stack
{
Public:
Void Push (int elem)
Int Pop (void)
Int GetCount (void) const; / / const member function
Private:
Int m_num
Int m_data [100]
}
Int Stack::GetCount (void) const
{
+ + m _ numb; / / compilation error in an attempt to modify data member m_num
Pop (); / / compilation error attempted to call non-const function
Return m_num
}
The declaration of the const member function looks strange: the const keyword can only be placed at the end of the function declaration, probably because everything else is already occupied.
A few rules about the Const function:
A. const objects can only access const member functions, while non-const objects can access any member functions, including const member functions.
B. members of const objects are immutable, while objects maintained by const objects through pointers can be modified.
C. const member functions cannot modify the data of an object, regardless of whether the object has a const property or not. When compiling, it checks based on whether to modify the member data.
e. However, the data member with the mutable modifier can be modified in any case by any means, and naturally the const member function at this time can modify it.
This is the end of the introduction of "Const,Const function in C++, what is the concept of Const variable". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.