The problem between const and pointer, reference, member method in C++

The type modified by const is the closest formed type to him, and the rest are what he modified

1. The relationship between const and pointer

1.1 The problem of forced conversion of constant const and pointer

①If a constant variable is assigned to a variable, then C++ implements the substitution principle when compiling, and assigns the value of the constant variable directly to the variable.
The following example problem (paste the code into the drawing board):insert image description here

In the first main function, which is our code, assign the constant variable a to the variable b, then when C++ compiles, it will replace a with 10, and then assign it to b. After the replacement, the second main function in the figure above, Then it will output: a=10,b=10,*p=100.

However, the compilation rules of .c are different from those of C++. .C will assign the value after forced conversion to b, so when *p becomes 100, then a is also 100, so b is equal to the value changed after a forced conversion 100 , the final output: a=100,b=100,*p=100.

Question: How to know whether our program is compiled by C method or by C++ method?
Which way to import through __cplusplus
: insert image description here
which way to print:insert image description here

②Remove the permanence and force the change
Explanation: remove the permanence of the variable
insert image description here

1.2 Deep understanding

The understanding of "ability can be contracted but not expanded", which of the following sentences can be compiled and cannot be compiled?

int a=10,b=20;
const int *p=&a;//这里的const修饰之后,p的指向可以改变,但是*P不能改变

//下面四句话中,s0,s3不可以编译通过,因为这两行会把*p的能力进行扩展
int *s0=p;
const int *s1=p;
int *const s2=p;
const int * const s3=p;
int a=10,b=20;
int* const p=&a;//这里的const修饰之后,p的指向不能进行改变,但是*p可以改变

//下面四句话都可以编译通过,因为p的指向被限制了,但是其他指针可以指向p,也可以指向其他的地址,并不会因为p的指向固定而被限制
int *s0=p;
const int *s1=p;
int * const s2=p;
const int * const s3=p;

2. The relationship between const and reference

2.1 Basic Concepts:

Ability can be contracted, for example:

int main()
{
    
    
    int a=10;
    int& b=a;
    const int&c=a;
    a+=100;//编译通过
    b+=100;//编译通过
    //c+=100;//编译出错,因为前面有const进行修饰,所以c不能改变
}

The ability cannot be extended, for example:

int main()
{
    
    
    const int a=10;
    //int& b=a;//这里编译不通过
}

2.2 The relationship between pointer reference and const

①Example:
insert image description here
For the code in the green box above, if s=&b, p2 will also point to b, so the value of b is 20.

②Exercise:
For the following statements, which can be compiled and which cannot be compiled?
(The answer is already given in the picture below)
insert image description here
insert image description here

2.3 The nature of citations

①Explanation:
The code before compilation is as follows: The code after compilation is as follows:
insert image description here

Difference between reference and pointer?
Syntax: references are aliases for variables (reference safe)
Assembly/Low level: references are equivalent to pointers that are themselves const

②Which of the following output x, y, z is correct?
Answer: Only x is true.
Although y also prints 20, the value of the local variable address is not disturbed. If the address of the local variable is fully allocated and reassigned to the local variable address, the printed y is no longer 20.
z will print a random value, because cout will perturb the stack frame space reference returned by funref before the release of
insert image description here
the address of the local variable, and this address will be released to get a random value. (Cannot get the address from the invalid space)
Summary: How to return the variable as a reference?
This variable is not affected by the lifetime of the function
Global variables
Static local variables Variables
entered by reference

For example:
insert image description here

2.4 Can a function be used to implement both get and set?

//实现双向函数
class Object
{
    
    
private:
    int value;
public:
    Object(int x = 0) :value(x) 
    {
    
    
    }
    ~Object()
    {
    
    
    }
    void SetValue(int x)
    {
    
     
        value = x; 
    }
    int GetValue() 
    {
    
     
        return value; 
    }
    // 使用一个函数实现 SetValue 和 GetValue() 函数的功能
    int& Bidirectional_function()
    {
    
    
        return value;
    }
    const int& Bidirectional_function()const//常对象调用常方法
    {
    
    
        return value;
    }
    //上面两个成员方法是函数重载。因为虽然函数名称一样,形参个数一样,但是形参的类型不一样
};
void fun(const Object& obj)
{
    
    
    const int& x = obj.Bidirectional_function();
}
int main()
{
    
    
    Object obj(10);
    int x = 0;
    x = obj.Bidirectional_function();//获取成员变量的值
    obj.Bidirectional_function () += 100;//改变成员变量的值
}

3. The relationship between member methods defined in classes and const

class CGoods
{
    
    
private:
    char Name[21];
    int Amount;
    float Price;
    float Total_value;
public:
     void RegisterGoods(char[],int,float);
     void CountTotal(void);
     void GetName(char[]);
     int GetAmount(void);
     float GetPrice(void);
     float GetTotal_value(void);
}

①For the above class, if you add const to one of the member methods, then this method is a regular method, because after the const is put in, it means that the member attributes in the member method of the object cannot be changed as follows:

void CountTotal(void)const

②If you don't want to change the this pointer, you need to add a const, as follows:

//为了和常方法的const进行区别,这里加上this指针,所以为了使得this指针不改变,给this前面加上const
//所以第一个const表示常方法,这个类的成员属性只能读,不能改
//第二个const表示这个this指针指向当前的对象,不能指向其他的对象
void CountTotal(const CGoods* const this)

③Add const before the method to indicate that the returned value is constant

const  float GetPrice(void);

④Why make some member methods a regular method?
Reason: You can use normal objects to call regular methods, and you can use regular objects to call regular methods, so that a function can be used in a wider range of places.

おすすめ

転載: blog.csdn.net/m0_54355780/article/details/122371957