Advertisement
Ginsutime

Move Assignment Operator Full Cherno

Feb 19th, 2022
1,131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <memory>
  4.  
  5. class String
  6. {
  7. public:
  8.     String() = default;
  9.     String(const char* string)
  10.     {
  11.         printf("Created!\n"); // 'f' at the end of the function name refers to “formatted”,
  12.                               // indicating that the function requires a format string in addition to the values to be printed.
  13.         m_Size = strlen(string);
  14.         m_Data = new char[m_Size];
  15.         memcpy(m_Data, string, m_Size);
  16.     }
  17.  
  18.     String(const String& other)
  19.     {
  20.         printf("Copied!\n");
  21.         m_Size = other.m_Size;
  22.         m_Data = new char[m_Size];
  23.         memcpy(m_Data, other.m_Data, m_Size);
  24.     }
  25.  
  26.     String(String&& other) noexcept // Move constructor, means Cherno in Entity entity("Cherno"); becomes temporary
  27.     {
  28.         printf("Moved!\n");
  29.         m_Size = other.m_Size;
  30.         m_Data = other.m_Data; // taking the pointer to m_Data (existing buffer in og string) and saying
  31.                                // char* m_Data; points to the same data
  32.         // Can't end move constructor here, have to take care of the other string instance who you take control of/steal from
  33.         // We make a hollow object doing this, shallow copy with rewired pointers basically
  34.         other.m_Size = 0;
  35.         other.m_Data = nullptr;
  36.     }
  37.  
  38.     String& operator=(String&& other) noexcept
  39.     {   // Not constructing new object like in the move constructor above, we're moving another object into this current object
  40.         // Meaning we need to overwrite the current object
  41.         printf("Moved!\n");
  42.  
  43.         if (this != &other) // Making sure we can't move the current object into itself. AKA same object.
  44.         {
  45.             delete[] m_Data; // Have to add this because we're about to move another object into ourselves and the old data has to be deleted
  46.  
  47.             m_Size = other.m_Size;
  48.             m_Data = other.m_Data;
  49.  
  50.             other.m_Size = 0;
  51.             other.m_Data = nullptr;
  52.         }
  53.  
  54.         return *this; // reference to current object
  55.     }
  56.  
  57.     ~String()
  58.     {
  59.         printf("Destroyed\n");
  60.         delete[] m_Data; // Done with [] since m_Data = new char[m_Size]
  61.     }
  62.  
  63.     void Print()
  64.     {
  65.         for (uint32_t i = 0; i < m_Size; i++)
  66.             printf("%c", m_Data[i]);
  67.  
  68.         printf("\n");
  69.     }
  70. private:
  71.     char* m_Data;
  72.     uint32_t m_Size;
  73. };
  74.  
  75. class Entity
  76. {
  77. public:
  78.     Entity(const String& name)
  79.         : m_Name(name)
  80.     {
  81.     }
  82.  
  83.     Entity(String&& name) // Should be called instead of other if an rvalue is used
  84.         : m_Name(std::move(name)) // Name is "Cherno" from entity below
  85.     {
  86.     }
  87.  
  88.     void PrintName()
  89.     {
  90.         m_Name.Print();
  91.     }
  92. private:
  93.     String m_Name;
  94. };
  95.  
  96. int main()
  97. {
  98.     //Entity entity("Cherno");
  99.     //entity.PrintName();
  100.  
  101.     String apple = "Apple";
  102.     String dest;
  103.  
  104.     std::cout << "Apple: ";
  105.     apple.Print();
  106.     std::cout << "Dest: ";
  107.     dest.Print();
  108.  
  109.     // Moves contents of apple into destination and leave apple in a valid state
  110.     dest = std::move(apple); // Have to do this way, not dest = apple;
  111.  
  112.     std::cout << "Apple: ";
  113.     apple.Print();
  114.     std::cout << "Dest: ";
  115.     dest.Print();
  116.  
  117.     std::cin.get();
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement