c++ - const and its position w/o pointers or references are present

本文探讨了C++中const修饰符的位置对于变量声明的影响,特别是在涉及指针和引用时的不同情况。通过实例展示了const修饰符放置的不同位置如何影响代码的编译和运行时行为。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

In C++, the poisition of const has special rules with/without the pointer is present.

 

Let's first see some examples that shows that the position of the const is irrelevant in terms of reference or ordinal Type declaration without pointer.

 

 

let 's see the example :

 

suppose we have function template, which is as follow. 

 

int main3(int argc, _TCHAR* argv[]) { 

	// it does not matter if you have int const or const int
    const int c_a = 0;
	int const c_a2 = 0;

	// nor does it matter if you have const int & or int const & 
	const int &r_ca = c_a;
	int const &r_ca2 = c_a;

	// the thing that get tricker is when the pointer is involved
	// ponter to array
	int *p_ia = ia;
	// point to const array
	const int *p_cia = ia;

	// reference to pointer to const array
	const int *&r_cia = p_cia;
	// reference to pointer to const array
	int const *&r_cia2 = p_cia;

	// const reference to pointer to array
	int * const &r_cia3 = p_ia;
	//int * &const r_cia4 = p_ia; // this is a compilation warning, chronical use error, wrong qualification

	// const reference to pointer to const array
	const int * const & r_cia4 = p_cia;

	// you cannot change the value of r_cia3 and r_cia4
	//r_cia3 = 0; // error: you cannot assign to const 
	//r_cia4 = 0; // error: you cannot assign to const

	return 0;
}
 

 

So as you can see, it does not matter if  you have int const of const int, nor does it matter if you have const int & or int const &;

 

Given the following function template definition;

 

template<typename Type, int size>
Type min(Type (&r_arr)[size]) { 
	Type min_val = r_arr[0];
	for (int i = 1; i < size; i++) { 
		if (r_arr[i] < min_val) {
			min_val = r_arr[i];
		}
	}

	return min_val;
}
 

 

Suppose that you want to manifest that there is no change to the array and the return value is a rvalue, then  you can add the const modifier 

 

template<typename Type, int size>
const Type min(const Type (&r_arr)[size]) { 
	Type min_val = r_arr[0];
	for (int i = 1; i < size; i++) { 
		if (r_arr[i] < min_val) {
			min_val = r_arr[i];
		}
	}

	return min_val;
}
 

but some readers may wonder what if I add in addition a reference symbols, such as this: 

 

template<typename Type, int size>
const Type&  min(const Type& (&r_arr)[size]) { 
	Type min_val = r_arr[0];
	for (int i = 1; i < size; i++) { 
		if (r_arr[i] < min_val) {
			min_val = r_arr[i];
		}
	}

	return min_val;
}
 

It won't compile as the error:

 

"r_arr: reference to reference is illegal"

"r_arr: array of reference is illegal"

 

 

as many user may have found out, as it does not make difference to have "const Type &" or "Type const &", so the following declaration is also wrong. 

 

template<typename Type, int size>
Type const&  min(Type const& (&r_arr)[size]) { 
    // ... 
}
 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值