[C++ Grammar Series] [1] Detailed Explanation of const

1. Ordinary const variable initialization

	int a = 9;
	const int ca = a;  // 非const赋值给const
	int b = a;         // const赋值给非const,如果是对象,则调用对象的operator=运算符,进行拷贝赋值
                       // b 是a的副本,b的修改不会影响到a,所以可以赋值

2. const reference variable

	int a = 9;
	const int & ca = a;  // 非const变量赋值给const引用

	const int b = 9;
	const int &cb = b;   // const变量赋值给const引用

	int c = cb;          // const引用赋值给非const变量
	//int &cc = cb;      // error,const引用不能赋值给非const引用,一旦可以赋值,则可以通过cc修改cb的值,这就破坏了cb是const引用的定义

3. const variable as function parameter

void f(int &a)
{
	cout << a << endl;
}

void fc(const int a)
{
	cout << "fc a=" << a << endl;
}

	int a = 9;
	const int & ca = a;  // 非const变量赋值给const引用

	fc(a);  // 非const值传递给const变量,实际上调用对象的拷贝构造函数A(A a);
	fc(ca); // const引用传递给const变量,二者都不会修改ca的值
	//f(ca);  // 将const引用传递给非const引用参数,可以通过参数a修改ca的值,所以违背了ca的const属性,error

Summary 1: You can freely transfer between const constants and non-const constants, but const references cannot. Only a non-const reference can be passed to a const reference, and a const reference cannot be passed to a non-const reference.

One-sentence principle: Any practice that can modify the value of a const variable is illegal.

4. const member function

If the member function of the class does not need to modify the member variable of the class, it is generally set to const.

class A
{
public:
	A(int age) {
		this->age_ = age;
	};
	int  get()const {  // const用于修饰this,表示当前对象this的属性值不会被修改
		return this->age_;
	}
	void set(const int &age) {  // const修饰入参,并不是修饰this,所以this的属性可以被修改
		this->age_ = age;       // 调用对象的operator=方法,进行数据赋值。c++中,如果类没有定义operator=方法,编译器会默认合成一个。
	}
private:
	int age_;
};
int main(int argc, int * argv[])
{


	int a = 9;
	const int & ca = a;  // 非const变量赋值给const引用
	A tom(10);
	cout << tom.get() << endl;
	tom.set(ca);
	cout << "main" << endl;
	system("pause");
}

Summary: const is generally used in engineering

1. Define constants to replace macro definitions.

2. Constant references in the parameters of ordinary functions.

3. The this pointer of the member function is set to const.

4. The constant reference of the input parameter of the member function.

 

In C++, there are two points to note

    // A a;
	// A b;
	// a = b; 这里实际调用A的operator=方法,进行数据拷贝,如果A没有显示定义该方法,则编译器会自动合成一个operator=方法
    // A a;
	/*
	void f(A a);
	f(a); 这里的参数时值传递,会调用A的拷贝构造函数,进行数据拷贝。A(A a);如果A没有显示定义该构造函数,则编译器会自动合成一个
	*/

It can be seen that the copying of objects in C++ is actually done silently by these two functions, even if these two interfaces are not written. In a sense, this default approach is for this kind of function parameter value passing and object assignment. However, if there is a pointer to the attribute of the object, there will be a problem, which will lead to the concept of shallow copy and deep copy; if some objects do not want to be copied, such as the object is too large, copying is too resource-intensive, you can delete these two methods, Use =delete. This will be introduced later.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325896184&siteId=291194637