Advertisement
Ginsutime

Cherno Main Tutorial

Feb 21st, 2022
878
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.52 KB | None | 0 0
  1. #include <iostream>
  2. #include "Array.h"
  3. #include "Vector.h"
  4.  
  5. struct Vector3
  6. {
  7.     float x = 0.0f, y = 0.0f, z = 0.0f;
  8.     int* m_MemoryBlock = nullptr;
  9.  
  10.     Vector3() { m_MemoryBlock = new int[5]; }
  11.     Vector3(float scalar)
  12.         : x(scalar), y(scalar), z(scalar) {
  13.         m_MemoryBlock = new int[5];
  14.     }
  15.     Vector3(float x, float y, float z)
  16.         : x(x), y(y), z(z) {
  17.         m_MemoryBlock = new int[5];
  18.     }
  19.  
  20.     Vector3(const Vector3& other) = delete;
  21.  
  22.     Vector3(Vector3&& other) noexcept // noexcept not part of Cherno's code but compiler's telling me to
  23.         : x(other.x), y(other.y), z(other.z)
  24.     {
  25.         m_MemoryBlock = other.m_MemoryBlock;
  26.         other.m_MemoryBlock = nullptr;
  27.         std::cout << "Move\n";
  28.     }
  29.  
  30.     ~Vector3()
  31.     {
  32.         std::cout << "Destroy\n";
  33.         delete[] m_MemoryBlock;
  34.     }
  35.  
  36.     Vector3& operator=(const Vector3& other) = delete;
  37.  
  38.     Vector3& operator=(Vector3&& other) noexcept // noexcept not part of Cherno's code but compiler's telling me to
  39.     {
  40.         m_MemoryBlock = other.m_MemoryBlock;
  41.         other.m_MemoryBlock = nullptr;
  42.         std::cout << "Move\n";
  43.         x = other.x;
  44.         y = other.y;
  45.         z = other.z;
  46.         return *this;
  47.     }
  48. };
  49.  
  50. template<typename T>
  51. void PrintVector(const Vector<T>& vector)
  52. {
  53.     for (size_t i = 0; i < vector.Size(); i++)
  54.         std::cout << vector[i] << std::endl;
  55.  
  56.     std::cout << "-----------------------------------------------\n";
  57. }
  58.  
  59. template<>
  60. void PrintVector(const Vector<Vector3>& vector)
  61. {
  62.     for (size_t i = 0; i < vector.Size(); i++)
  63.         std::cout << vector[i].x << ", " << vector[i].y << ", " << vector[i].z << std::endl;
  64.  
  65.     std::cout << "-----------------------------------------------\n";
  66. }
  67.  
  68. int main()
  69. {
  70.     Array<std::string, 2> data;
  71.     data[0] = "Cherno";
  72.     data[1] = "C++";
  73.  
  74.     {
  75.         Vector<Vector3> vector;
  76.         vector.EmplaceBack(1.0f); // Removes need for (Vector3(1.0f)); by doing construction there and then
  77.         vector.EmplaceBack(2, 3, 4);
  78.         vector.EmplaceBack(1, 3, 4);
  79.         vector.EmplaceBack();
  80.         PrintVector(vector);
  81.         vector.PopBack();
  82.         vector.PopBack();
  83.         PrintVector(vector);
  84.         vector.EmplaceBack(5, 2, 0);
  85.         vector.EmplaceBack(1, 7, 9);
  86.         PrintVector(vector);
  87.  
  88.         vector.Clear();
  89.         PrintVector(vector);
  90.         vector.EmplaceBack(5, 2, 0);
  91.         vector.EmplaceBack(1, 7, 9);
  92.         PrintVector(vector);
  93.  
  94.         PrintVector(vector);
  95.     }
  96.  
  97.     Vector<int> intVector;
  98.     intVector.PushBack(5);
  99.     intVector.EmplaceBack(2);
  100.     intVector.EmplaceBack(2);
  101.     intVector.EmplaceBack(2);
  102.     intVector.EmplaceBack(2);
  103.     intVector.EmplaceBack(2);
  104.     PrintVector(intVector);
  105.     intVector.PopBack();
  106.     PrintVector(intVector);
  107.     intVector.Clear();
  108.  
  109.     std::cin.get();
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement