Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions const -- types/Log.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include <iostream>
#include <string>

class Entity
{
private :
int m_X, m_Y;
mutable int var;
public :
//const int* const GetX() const //[means cant change the content and cant reassign pointer also cant change member variables of the class through methods]
int GetX() const
{

var = 2;//suppose it needa to modify keep mutable in front of the variable
return m_X;
}

int GetX()
{
return m_X;
}

/*keeping const after method in class means
this method cannot modify actual class and its
belonged variables makes just a
read only method*/

void SetX(int x)// const makes no sense its setting
{
m_X = x;
}

};

void PrintEntity(const Entity& e)//pass it by conast refernece to avoid copying [just a read only memory]
{
/* e = nullptr;
e = Entity();

its not like pointers
reassigning this object means changing its own
no separation between pointer and contents of the pointer
cause you are the contents in ref
you are the entity though you are a ref
and GetX funtion need to be const otherwise it violates const Entity */

std::cout << e.GetX() << std::endl;

}


int main()
{

Entity e;
const int MAX_AGE = 90;

const int* const a = new int;

*a = 2;
a = (int*)&MAX_AGE;
a = nullptr;


std::cout << *a << std::endl;

std::cin.get();



}


//int* m_X, *m_Y //its like both considering pointers
29 changes: 29 additions & 0 deletions const -- types/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <iostream>
#include <string>

int main()
{
const int MAX_AGE = 5;//this integer is a constant you cannot change it!

int* a = new int; //created on heap tO acquire a pointer

*a = 2;
a = (int*)&MAX_AGE;

int const* a = new int;//contents cant be changed '*a'
const int* a = new int;//same as the above

int*const a = new int;//cant reassign the pointer 'a'
const int*const a = new int;//contents and assigning of pointer cant be changed



/*dereferencing and equating to 2 [contents]
reassigning pointer to some other memory adress*/

std::cout << *a << std::endl;//reading 'a' is fine



std::cin.get();
}
Binary file added const -- types/main.exe
Binary file not shown.