diff --git a/const -- types/Log.cpp b/const -- types/Log.cpp new file mode 100644 index 0000000..06ac31f --- /dev/null +++ b/const -- types/Log.cpp @@ -0,0 +1,74 @@ +#include +#include + +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 \ No newline at end of file diff --git a/const -- types/main.cpp b/const -- types/main.cpp new file mode 100644 index 0000000..ed5e4c7 --- /dev/null +++ b/const -- types/main.cpp @@ -0,0 +1,29 @@ +#include +#include + +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(); +} \ No newline at end of file diff --git a/const -- types/main.exe b/const -- types/main.exe new file mode 100644 index 0000000..6918168 Binary files /dev/null and b/const -- types/main.exe differ