summaryrefslogtreecommitdiff
path: root/const-pointer-tests.cpp
blob: dceda161652dfc9b2d3b4b2d688ec43ab321e3f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

/*
 *
 * Trying to make sense of const
 *
 *
 */

int main () {
	int a = 0;
	int b = 0;
	int c = 0;
	int const* pointer1 = &a; // Cannot change underlying object, can point to other object (Please for the love of god dont use this version its so confusing, use pointer3 version instead because its the same thing and much more clear
	int *const pointer2 = &b; // Can change underlying object, can not point to new object
	const int* pointer3 = &c; // Cannot change underlying object, can point to other object
	std::cout << pointer1 << "\n"
			  << pointer2 << "\n"
			  << pointer3 << std::endl;

	const int *const pointer4 = &a; // Cannot change underlying object, cannot point to another object
	std::cout << pointer4 << std::endl;
	return 0;
}