Yesterday interview by the Group of Examiners asked about the const and define similarities and differences, began to be simple, then asked my head is a group of paste. Almost a summary of the information today
1. Const and define.
Both can be used to define constants, but the const definition defines the type of the constant, so it is more accurate. #define只是简单的文本替换, in addition to defining constants, can also be used to define some simple functions, somewhat like built-in functions. Constants defined by const and define can be placed inside the header file. (Small bet: can be declared more than once, but only once)
2. Const with pointers and references.
(a) const and pointer. Let 's take a look at some of the following definitions:
1 intme;2 Const int* p1=&me;//p1 variable, *p1 immutable, but cannot be modified with *P1, p1 can turn3 int*Constp2=&me;//The P2 is immutable, *p2 variable, allowing *P2 to modify its value, but P2 cannot turn. 4 Const int*Constp3=&me;//P3 immutable, *P3 also immutable, at this time can not use *P3 to modify its value, nor to turn5 //The first const means that for P1 it is pointing to the const constant, although me is not, but for P1.
(b) The difference between a pointer and a reference is simply that the reference is more concise and more secure. Because the reference declaration must be initialized. The reference is closer to the const pointer, and once associated with a variable, it will remain faithful to him.
(c) Const pointers can accept const and non-const addresses, but non-const pointers accept only non-const addresses. So the const pointer is more powerful, so use the const pointer as much as possible, which is a habit. 3.(a) const and function. Because of C, the formal parameter types of functions are often set to const, and many are const references. However, there is a restriction that you cannot pass an address that is not an lvalue to a reference. (Lvalue includes variables, array elements, struct members, references, dereferenced pointers, etc.). The parameter is a const type, stating that the function will not modify its value, which is the const function. (b) Const and class member functions. Let's take a look at the following code:
1 const Stock land =stock ( " hyd " 2 land.show (); 3 Land is a constant, but the class member function show cannot guarantee that land is not modified, so the compiler will refuse to execute the code. 4 // 5 Show () const ;(declaration) 6 void stock::show () const {} (definition )。
(ii) Detailed introduction, Attention to detail
Common in C: "#define Variable name variable value" defines a value substitution, but there is a fatal disadvantage: the lack of a type detection mechanism, so that preprocessing in C + + becomes a potential cause of errors, so the introduction of Const.
Const USE:
1. Two cases for pointers: const is a left-associative type modifier.
int const *A; A variable, *a not variable
int *const A; A immutable, *a variable
2. Pass-through value parameters of the qualifying function:
void function (const int Var); The passed arguments cannot be changed within the function.
3. The qualifying function returns a value type.
const int function (); The const is meaningless at this time
Const myclassname function (); The function returns the custom type myClassName.
4 Qualifying function type.
void function () const; Constant member function, the const member function cannot change the member function of an object.
For example:
int point::gety (){return yval; 4}
When this function is called, the point object is not changed, and the following function changes the point object:
1 void Point:: SETPT (intint y)2{3 xval=x; 4 yval=y; 5 }
To make the meaning of the member function clearer, we can add a const description to the function prototype that does not change the member function of the object:
1 class Point2 {3 Public:4 intGetX ()Const;5 intGetY ()Const;6 voidSETPT (int,int);7 voidOFFSETPT (int,int);8 Private:9 intxval, Yval;Ten};
The const member function should add a const qualification to both the function prototype description and the function definition:
1 intPoint::gety ()Const2 {3 returnYval;4 }5 6 classSet {7 Public:8Set (void) {card =0; }9 BOOLMember (Const int)Const;Ten voidAddelem (Const int); One //... A }; - - BOOLSet::member (Const intElemConst the { - //... -}
A const member function cannot be called by a constant member object because it may attempt to modify a constant's data member:
Const SET S;
S.addelem (10); Illegal: Addelem is not a constant member function
S.member (10); That's right
However, constructors and destructors have exceptions to this rule, which are never defined as constant members, but can be called by constant objects (called automatically). They can also assign values to the data members of a constant, unless the data member itself is a constant.
Why do I need a const member function?
In the member functions of the class we define, there are often some member functions that do not change the data members of the class, that is, the functions are "read Only" functions, and some functions modify the values of the class data members. If a function that does not change the data member is labeled with the Const keyword, it is obvious that the readability of the program can be improved. In fact, it can improve the reliability of the program, has been defined as a const member function, once the attempt to modify the value of the data member, the compiler is handled by error.
Const member functions and const objects
In fact, the const member function has another function, which is the constant object correlation. For built-in data types, we can define their constants, as well as user-defined classes, which can define their constant objects. For example, a method that defines an integral type constant is:
const int I=1;
Similarly, you can define a const object, assuming that there is a class ClassA, the method that defines the constant object of the class is:
Const ClassA A (2);
Here, A is a const object of class ClassA, and "2" is passed to its constructor parameter. The data members of a const object cannot be changed during the lifetime of the object. But how do you ensure that the data members of this class are not changed?
To ensure that the data members of a const object are not changed, the const object can only call the const member function in C + +. If a member function does not actually make any kind of modification to the data member, it is not qualified by the Const keyword and cannot be called by a constant object. Here's an example to illustrate the problem:
1 classC2 {3 intX;4 Public:5 intGetX ()6 {7 returnX;8 }9 voidSetX (intX)Ten { One This->x =X; A } - }; - the voidMain () - { - ConstC CONSTC; -cout<<constc.getx (); +}
If we compile the program code above, the compiler will get an error: CONSTC is a constant object, it can only call the const member function. Although the Getx () function does not actually change the data member X, it cannot be called by the CONSTC object because there is no const keyword qualification. If we put the above code in:
int GetX () rewritten as:int GetX () constAnd then recompile, there's no problem. use of the const member function the const member function indicates that the member function can read only class data members, and cannot modify class member data. When defining a const member function, place the const keyword between the function's parameter table and the function body. One might ask: why not put a const before a function declaration? Because doing so means that the return value of the function is constant and the meaning is completely different. Here is an example of defining a const member function:
1 class X 2 {3 int i; 4 Public : 5 int Const ; 6 };
The keyword const must be repeated in the function implementation in the same way, or the compiler will think of it as a different function:
int x::f () const { return i; } if f () attempts to change I in any way or call another non-const member function, the compiler will give an error message. Any function that does not modify member data should be declared as a const function, which helps to improve the readability and reliability of the program.
C + + const and define