Const qualifier: Use const to qualify the object so that the const object cannot be changed once it is created.A const object must be initialized when it is defined, in two ways:const int x = 10;const int x = GetValue ();The first is to assign a
??? ??Problem:
Can the const member function and the ordinary member function be the same with the same parameter? Yes, this is an overload of a function.
Can the const member function modify the value of a member variable of an object?
The concept of const is a variable that contains values that cannot be modified.A constant expression is an expression that can be fully evaluated at compile-time. Therefore, constants cannot be initialized from values that are extracted from a
The standard int const A is exactly equivalent to the const int A. Because of this, there will be a lot of different styles, the same "* is the type or variable?" ", such as char* P and Char *p, they are equivalent. Is it using the const t& T or T
C ++ const data member
A member constant modified by const. The memory address of this constant is in the constant area. If the value of the member constant is determined after the structure of the object. That is, the constant belongs to an object.
when const encounters a pointerIn general, the const modifier pointers can be divided into the following concentration cases.
Describe
Example
Meaning
Note
Const on the left of *
const INT *b=&a;int
classProgram { PublicProgram (inty) { This. y =y; } Public Const intx =123;//The default is a static value, which must be initialized at declaration time, only string and null for the reference type to be declared as const Public
The difference between const and static const--read-only, used only in declarations1 Const int Ten ; 2 int Const ten;Num variable is read-only and cannot be modified1 Const int arr[4] = {ten ) ; 2 int Const arr[4] = {ten , +} ;The value of an
First, the conclusion .
The assignment of a non-reference type does not matter Const.
Const references can point to const and non-Const. But non-const references can only point to non-Const.
A pointer to a const that can point to
//two usages of const-modified references#include using namespacestd;structteacher{Charname[ -]; intAge ;};voidSetA (ConstTeacher &T1) { //t1.age = 13; //Error C3490: cannot be modified because "age" is being accessed through a const
In the C + + class definition, you can see a definition similar to the following:Class List {PrivateNode * P_HEAD;int length;......Public:int getlength () const;BOOL Getnodeinfo (const int Index,node & buffer) const {...}BOOL Deletenode (const int
This blog is based on Bo Qian's YouTube tutorial, which you can find on YouTube and a summary.This section summarizes the understanding of the const:Learn C + + know that the const in C + + is used to modify constants, it means that the immutable,
Const is the abbreviation of constant, the meaning of "constant unchanged". The const-modified items are protected against accidental changes and can improve the robustness of the program. So many C + + programming books suggest: "Use const whenever
The const in C is a run-time const that is only defined at compile time and is initialized only at run time, which is why Const cannot be used to define an array, and the value of const can be dynamically modified.Const in C + + is compile-time
Break C ++ Const rules and cconst rules
Starting with a C ++ cainiao Function
1 CString MyClass::GetStringValue() const2 {3 return m_strValue; 4 }
This value may not have been assigned a value. Well, I will first judge whether it is null. If it
C ++ const member functionsFirst fact:
In a certain type, two functions can be declared and defined in this way, and can be overloaded)
void pa(){ cout
The above statement is correct.
Based on this fact, I thought about its mechanism.
Experiments
Tag: A modifier reference is passed how C + + fine memory allocation connection definitionThe use of the Const keyword in C + + is very flexible, and using the const will greatly improve the robustness of the program, I found in various aspects of
Because C + + string is not compatible with string in C #, the data can only be passed with char* in the most C + + and sbyte* in C #.The sample code is as follows: string s = "Hello World.txt"; IntPtr ptr =
The string class defines a char*-to-string conversion function, which allows a C-style string to be used to initialize a string object.
One of the parameters of a parameter of type const reference indicates that: Assuming that the argument type
C + + const allows a semantic constraint to be specified, and the compiler enforces this constraint, allowing the programmer to tell the compiler that a value is unchanged. If there is a value in programming that does not change, you should
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.