From 5f27dcd05e1f95523087210d536f17ee47feb1c3 Mon Sep 17 00:00:00 2001 From: Butcher3Years Date: Wed, 11 Feb 2026 01:04:12 +0530 Subject: [PATCH] All about arrow operator and its Overloading --- Arrow Operator/Log.cpp | 53 ++++++++++++++++++++++++++++++++++++ Arrow Operator/main.cpp | 56 +++++++++++++++++++++++++++++++++++++++ Arrow Operator/offset.cpp | 26 ++++++++++++++++++ 3 files changed, 135 insertions(+) create mode 100644 Arrow Operator/Log.cpp create mode 100644 Arrow Operator/main.cpp create mode 100644 Arrow Operator/offset.cpp diff --git a/Arrow Operator/Log.cpp b/Arrow Operator/Log.cpp new file mode 100644 index 0000000..5eab9ef --- /dev/null +++ b/Arrow Operator/Log.cpp @@ -0,0 +1,53 @@ +#include +#include + +class Entity +{ +public : + int x; + + public : + void Print() const //need a const method to process on later + { + std::cout << "Hello!" << std::endl; + } +}; + +class scopedptr +{ +private : + Entity* m_Obj; + public : + scopedptr(Entity* entity) + :m_Obj(entity) + { + } + + ~scopedptr() + { + delete m_Obj; + } + + //Entity* GetObject() { return m_Obj; } + Entity* operator->()//we are overloading it + { + return m_Obj; + } + + const Entity* operator->() const + { + return m_Obj; + } +}; + + + +int main() +{ + const scopedptr entity = new Entity();//like Entity* entity = new Entity(); and for const scoped we need const version of the overloading + //entity.GetObject()->Print(); + entity->Print(); + + std::cin.get(); +} + diff --git a/Arrow Operator/main.cpp b/Arrow Operator/main.cpp new file mode 100644 index 0000000..a620f94 --- /dev/null +++ b/Arrow Operator/main.cpp @@ -0,0 +1,56 @@ +#include +#include + +class Entity +{ +public : + int x; + + public : + void Print() const + { + std::cout << "Hello!" << std::endl; + } +}; + +class scopedptr +{ +private : + Entity* m_Obj; + public : + scopedptr(Entity* entity) + :m_Obj(entity) + { + } + + ~scopedptr() + { + delete m_Obj; + } +}; + + + +int main() +{ + + Entity e; + e.Print(); + + + Entity* ptr = &e; + + ptr->Print();//DEREF Entity ptr + + ptr->x = 2; + //Entity& entity = *ptr;//DEREF + //ptr.Print();//JUST A POINTER or we can do (*ptr).Print(); + //entity.Print(); + + + + std::cin.get(); +} + +// ABOUT HOW ARROW OPERATOR AFFECTS STRUCT AND CLASS POINTERS +// AS an Operator we can overload it and use it on our own custom classes \ No newline at end of file diff --git a/Arrow Operator/offset.cpp b/Arrow Operator/offset.cpp new file mode 100644 index 0000000..a71ec7b --- /dev/null +++ b/Arrow Operator/offset.cpp @@ -0,0 +1,26 @@ +#include +#include + +struct Vector3 +{ + float x, y, z;//if x,z,y we have different layout in memory even though same kinda class +}; + + +int main() +{ + int offset = (int)&((Vector3*)0)->y;//kinda give me a invalid memory without ref and after it will give offset + std::cout << offset << std::endl; + + + /*or + &((Vector3*)nullptr)->x; + */ + + std::cin.get(); +} + +//suppose we need to find offset of this y variable was in the memory +//x is 0 y is 4 and z is 8 bytes in the struct! +//in C++, offset refers to the distance, measured in bytes, from a reference point—typically the beginning of a data structure (like a struct or class)—to a specific member or memory location. +//useful when we serialising data in game engines