PrevUpHomeNext

day-10: c++ Reference, Constant


cpp Reference, Constant

c++ reference

c++ constant: const

cpp reference

What is c++ reference? In brief, c++ reference is name alias.

// a, c, e, f are variables,
// any one of them has its unique memory space and its unique memory address.

// b is alias of a, b is just another name of the same memory space that a stands for.

// d is alias of c, d is just another name of the same memory space that c stands for.

class one_class {};

int a;
int & b = a;
one_class c;
one_class & d = c;
int e;
one_class f;

Constant

c++ constant: const

c++ const and reference are two different concepts, but they are always used together.

Variable means it is mutable, but marking the variable const means making it unmutable.

int a = 3;
a = 5;	// a is mutable, because a is a variable

const int b = 4;

// Although b is a variable, but because b is marked const, b is unmutable.

// b = 6;	// error, b is unmutable.

const and reference

Use c++ const and reference together.

-)

#include <iostream>

class one_class
{
private:
	int x;
public:
	void ma(int x1)
	{
		// x1 can be changed and then assigned to x

		this->x = x1;
	}
	void mb(const int x1)
	{
		// x1 can not be changed
		this->x = x1;
	}
	void mc(const int & x1)
	{
		// x1 can not be changed
		this->x = x1;
	}
};

int main()
{
	auto one = one_class{};
	
	int x2 = 9;

	one.ma(x2);

	// because x1 in .mb is "const int",
	// x1 will be created as a new memory space,
	// then copy value from x2 to x1,
	// and then copy value from x1 to "this->x"
	one.mb(x2);

	// because x1 in .mc is "const int &",
	// x1 will be an alias of x2,
	// then copy value from x2 to "this->x" directly.
	one.mc(x2);
}

-)

Some const reference rules

int a;	// a is non-const variable
const int b = 23;	// b is const variable, b must be initialized
const int a = 33;
const int & b = a;	// Correct!
int a;
int & b = a;	// Correct!
int a;
const int & b = a;	// Correct!
const int a = 33;
int & b = a;	// ERROR !!!

Too many rules? I tell you a secret, you can understand them by a simple logic:

Then you can deduce all of rules.

Written on Sep 20, 2024

Back to index

Index

cpp/c++

c++ std::exception:

std::cout.write(err.data(), err.size());

std::cout << std::endl;

caught:

  ===================================
  #  The c++ programming language.  #
  #                                 #
  #  Join c++ Discord: yZcauUAUyC   #
  #  Deck                           #
  ===================================

Home: cppfx.xyz

K


PrevUpHomeNext