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
53 changes: 53 additions & 0 deletions Arrow Operator/Log.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <iostream>
#include <string>

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();
}

56 changes: 56 additions & 0 deletions Arrow Operator/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <iostream>
#include <string>

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
26 changes: 26 additions & 0 deletions Arrow Operator/offset.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <iostream>
#include <string>

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