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
94 changes: 94 additions & 0 deletions Copying--Copying Constructor/Deep.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
//"DEEP COPYING" works through copying constrctor[is a constructor which called for second string when actually copies it ]

#include <iostream>
#include <string>
#include <cstring>

class String
{
private :
char* m_Buffer;
unsigned int m_size;//see size
public :
String(const char* string)
{
m_size = strlen(string);
m_Buffer = new char[m_size + 1 ];
memcpy(m_Buffer, string, m_size);
m_Buffer[m_size] = 0;

}

/* String(const String& other)//taking const ref to same class [copy constrctor] and we want memory in ptr not the pointer
: m_Buffer(other.m_Buffer), m_size(other.m_size)
{

}
this is default copy constrctor in c++
*/

String(const String& other)
: m_size(other.m_size)
{
std::cout << "Copied String!" << std::endl;
m_Buffer = new char[m_size + 1];//creating a new buffer
memcpy(m_Buffer, other.m_Buffer, m_size + 1);//same like before string class but a new buffer one also we know previous string have null termination
}


// String(const String& other) = delete; then coppy constrctor wont work same like unq ptr

/* OR

String(const String& other)
{
memcpy(this, &other, sizeof(String));
}
*/

~String()
{
delete m_Buffer;
}

char& operator[](unsigned int index)
{
return m_Buffer[index];
}

friend std::ostream& operator<<(std::ostream& stream, const String& string);
};


std::ostream& operator<<(std::ostream& stream, const String& string)
{
stream << string.m_Buffer;
return stream;
}

void PrintString(const String& string)//by attaching ref and const it wont copies again and we cant edit existing string
{
String copy = string;//power of const ref
std::cout << string << std::endl;
}

int main()
{

String string = "Cherno";
String second = string;

second[2] = 'a';

// std::cout << string << std::endl;
//std::cout << second << std::endl;

PrintString(string);
PrintString(second);//simply needlessly copying it and we actually copying it 2 more copies

std::cin.get();
}

//AFTER THIS SECOND STRING CANT CHANGE FIRST ONE
//By const ref we can do copies how much we wanted and this time copied String for string SECOND

62 changes: 62 additions & 0 deletions Copying--Copying Constructor/Log.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <iostream>
#include <string>
#include <cstring>

class String//strings made of an array of characters
{
private :
char* m_Buffer;
unsigned int m_size;//see size
public :
String(const char* string)
{
m_size = strlen(string);
m_Buffer = new char[m_size + 1 ];//with null termination character to get no extra memory adress

memcpy(m_Buffer, string, m_size);//destination ,souce and the size in bytes and size is + 1 if we not created null termination character down there
m_Buffer[m_size] = 0;//manually created the null termination character

/*for(int i = 0; i < m_size; i++)
m_Buffer[i] = string[i];//copies character one by one
best way is by memory copy */
}
~String()
{
delete m_Buffer;
}

char& operator[](unsigned int index)
{
return m_Buffer[index];
}

friend std::ostream& operator<<(std::ostream& stream, const String& string); //to avoid using another method GetBuffer and use priavte member we need to make a friend
};


std::ostream& operator<<(std::ostream& stream, const String& string) //overloading operator
{
stream << string.m_Buffer;
return stream;
}
int main()
{

String string = "Cherno";
String second = string;

second[2] = 'a';

std::cout << string << std::endl;
std::cout << second << std::endl;


std::cin.get();
}

//IN this copying compiler takes intial string class members list and copies them into a new memory adress second
//It does a shallow copy means the 2 strings have same char pointer means memory adresss is same in both strings in buffer
//At the end we want to delete the same block of adress twice so that we get error after the termination of {}
//To not to crash we need new char array to store copied string its like both pointing to same memory adress!!
//We need second block which conatain different pointer points a new memory adress!
//IK done through "DEEP COPYING" means not the shallow copying it does copy object Entirely
31 changes: 31 additions & 0 deletions Copying--Copying Constructor/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <iostream>
#include <string>


struct Vector2
{
float x, y;
};
int main()
{
Vector2* a = new Vector2{ 2, 3 };
Vector2* b = a; //here this new opearator affects both a and b cause those 2 ptrs points same memory address!!

//b->x = 2; affects memory adress not pointer

// b.x = 5;//a.x still 2 just copying a to b but still those 2 points different memory adressses


std::cin.get();
}

//By assigning one to other variable to other it always copies
//In pointers it copies the memory adress
//You cant re assign the refernces[just changing underlying thing thats pointing to ]

/* int a = 2 ;
int b = a;

int b = 3;//still a is 2
*/

2 changes: 1 addition & 1 deletion Smart pointers -- Types/weak.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ int main()

//shared ptr to shared ptr ref count increases
//shared ptr to weak ptr ref count remain same like you have Entities but you dont take ownership[it wont keep you alive]
//try to use unq ptr and then shared always
//try to use unq ptr and then shared always according to overhead