Advertisement
Ginsutime

Move Semantics Cherno

Feb 19th, 2022
834
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.04 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()
  39.     {
  40.         printf("Destroyed\n");
  41.         delete [] m_Data; // Done with [] since m_Data = new char[m_Size]
  42.     }
  43.  
  44.     void Print()
  45.     {
  46.         for (uint32_t i = 0; i < m_Size; i++)
  47.             printf("%c", m_Data[i]);
  48.  
  49.         printf("\n");
  50.     }
  51. private:
  52.     char* m_Data;
  53.     uint32_t m_Size;
  54. };
  55.  
  56. class Entity
  57. {
  58. public:
  59.     Entity(const String& name)
  60.         : m_Name(name)
  61.     {
  62.     }
  63.  
  64.     Entity(String&& name) // Should be called instead of other if an rvalue is used
  65.         : m_Name((String&&)name) // Copy made by doing m_Name(name), has to be the way on the left of this
  66.                                  // We explicitly cast it to a temporary
  67.         //In reality, you'd do m_Name(std::move(name)) instead for the code above
  68.     {
  69.     }
  70.  
  71.     void PrintName()
  72.     {
  73.         m_Name.Print();
  74.     }
  75. private:
  76.     String m_Name;
  77. };
  78.  
  79. int main()
  80. {
  81.     Entity entity("Cherno"); // Implicit version of Entity entity(String("Cherno"));
  82.     entity.PrintName();
  83.  
  84.     std::cin.get();
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement