1// various versions of const are explained below
2#include <iostream>
3class Entity {
4private:
5 int m_X, m_Y;
6 mutable int var; // can be modified inside const menthods
7 int* m_x, *m_y;// * use to create pointer in one line
8public:
9 int GetX() const // cant modify class variables
10 {
11 //m_X = 4;//error private member can't be modified inside const method
12 var = 5; // was set mutable
13 return m_X;
14 }
15 int Get_X()// will modify class
16 {
17 return m_X;
18 }
19 const int* const getX() const // returning a pointer that cannot be modified & context of pointer cannot be modified
20 {
21 //m_x = 4;
22 return m_x;
23 }
24 void PrintEntity(const Entity& e) {
25 std::cout << e.GetX() << std::endl;
26 }
27};
28int main() {
29 Entity e;
30 const int MAX_AGE = 90;
31 // MAX_AGE =100; error const var is stored in read only section in memory and we can't write to that memory
32 // int const* a = new int; is same as const int* a = new int ;////but you can't change the context of pointer but can reassign it to a pointer something else
33 int * const a = new int; //can change the context of pointer but can't reassign it to a pointer something else
34 *a = 2;
35 a = &MAX_AGE;// error can't change it to ptr something else
36 std::cout << *a << std::endl;
37 a =(int*) &MAX_AGE;
38 std::cout << *a << std::endl;
39}
1//Syntax: const Class_Name Object_name;
2const int MAXN = 1e5 + 5;
3const long long mod = (long long)1e9 + 7;