Explain const reference, constant pointer and pointer to constant in detail

  • Once the const object is created, its value cannot be changed, so the const variable must be initialized.
  • As long as the value of const is not changed, const variable is the same as other variables

const object in multiple files:

  • By default, objects decorated with const are only valid in files. This means that allowing const variables with the same name to be defined in multiple files is actually equivalent to defining independent variables in files. Examples are as follows:

main. The const variable MainSize is defined in CPP:

#include <iostream>
using namespace std;
extern void test();

const int MainSize = 100;
int main()
{
	int MainVal = MainSize;

	std::cout << "MainVal = " << MainVal << std::endl;
	test();

	system("pause");
	return 0;
}

test. The const variable MainSize is also defined in CPP:

#include <iostream>
const int MainSize = 200;
void test()
{
	int TestVal = MainSize;
	std::cout << "TestVal = " << TestVal << std::endl;
}

Results after operation:

  • So, how to use the same const variable in multiple files? That is, const is defined in only one file and declared and used in other files. The solution is to use the extern keyword, but unlike ordinary variables, extern must be added to the definition and declaration.

main.cpp defines the external const variable MainSize:

#include <iostream>
using namespace std;
extern void test();

extern const int MainSize = 100;
int main()
{
	int MainVal = MainSize;

	std::cout << "MainVal = " << MainVal << std::endl;
	test();

	system("pause");
	return 0;
}

test. The const variable MainSize is declared in CPP:

#include <iostream>

extern const int MainSize;
void test()
{
	int TestVal = MainSize;
	std::cout << "TestVal = " << TestVal << std::endl;
}

Operation results:

const reference

  • Non constant references to a constant object are not allowed. Because the meaning of constant object is unchanged. If you allow this syntax, you can change the constant object!
const int MainVal = 100;

const int& rMainVal = MainVal; //correct
int &rMainVal2 = MainVal;//error: cannot convert from 'const int' to 'int &'
  • When initializing a constant reference (a reference to const), any expression is allowed as the initial value.
int i = 42;
const int& r1 = i; //Allows constant references to be bound to an ordinary int object
const int& r2 = 55; //correct
const int& r3 = r1*2; //correct

Why does C + + allow the above operations? The reason is that the compiler will generate a const temporary variable during compilation. Finally, the references we want actually point to this temporary variable. Let's take a look at the example given in version 5 of C++ primer:

Combined with the above description, it is not difficult for us to think why the following initialization does not work: (because if it is allowed, modifying the value of ri or r3 only modifies the temporary variable and does not change the values of dval and r1, but our purpose is to change their values, so C + + defines this behavior as illegal)

  • example
#include <iostream>
using namespace std;

int main()
{
	int i = 42;
	const int& r1 = i;
	const int& r2 = 55;
	const int& r3 = r1 * 2;

	std::cout << "r2 = " << r2 << std::endl;
	
	std::cout << "&i =  " << &i  << " ; " << "i = " << i << std::endl;
	std::cout << "&r1 = " << &r1 << " ; " << "r1 = " << r1 << std::endl;
	std::cout << "&r3 = " << &r3 << " ; " << "r3 = " << r3 << std::endl;

	std::cout <<"change i After value"<< std::endl;

	i = 50;

	std::cout << "&i =  " << &i << " ; " << "i = " << i << std::endl;
	std::cout << "&r1 = " << &r1 << " ; " << "r1 = " << r1 << std::endl;
	std::cout << "&r3 = " << &r3 << " ; " << "r3 = " << r3 << std::endl;
	
	const int& r4 = r1 * 2;
	std::cout << "&r4 = " << &r4 << " ; " << "r4 = " << r4 << std::endl;

	system("pause");
	return 0;
}

Print results:

Constant pointers and pointers to constants

  1. const pointer
  2. pointer to const
  3. Combination of the two
  4. How to distinguish between constant pointers and pointers to constants
  • const pointer
    As the name suggests, it means that the pointer is a constant, and the pointer represents the address, so the constant pointer cannot change the pointing.
int errNumb = 0;
int myerrNumb = 1;

int *const curErr = &errNumb; //const pointers 
curErr = &myerrNumb;//Error: cannot assign value to constant
  • pointer to const
    Since it points to a constant, of course, its value cannot be changed, but the direction can be changed
const double pi = 3.14;
const double *cptr = &pi; //Defines a pointer to a constant
*cptr = 3.1415;//Error: cannot assign value to constant

double pi2 = 3.1415;
cptr = &pi2;//Change the direction, point to a non constant object
std::cout << *cptr << std::endl;

Print results:

  • Combination of the two

Namely: a constant pointer to a constant object. It has the characteristics of both: it can neither change the direction nor value.

  • How to distinguish between constant pointers and pointers to constants
int *const curErr = &errNumb; //const pointer 
const double *cptr = &pi; //pointer to const 

Look who const modifies. The curErr modified first stores the address. Of course, it points to the same; The second modification * cptr modifies a value. Of course, the value cannot be changed.

Keywords: C++ Programming pointer

Added by Bourgeois on Fri, 18 Feb 2022 04:10:03 +0200