/
lyapdy
/
skillbox_controllers
Обзор
Документация
Войти
/
lyapdy
/
skillbox_controllers
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
vector3d_project1/src/main.cpp
151 строка
5 KB
danillyapunov
task9 commit
13 дек 2025, 16:42
13 дек 2025, 16:42
916f5d0
Код
Авторство
О чём код?
#include "Vector3D.h" #include <memory> #include <vector> // Демонстрация семантики перемещения void demonstrateMoveSemantics() { std::cout << "\n=== Demonstration of Move Semantics ===" << std::endl; // Создание временного объекта Vector3D temp(1.0, 2.0, 3.0); // Использование конструктора перемещения Vector3D movedVec(std::move(temp)); std::cout << "After move constructor:" << std::endl; std::cout << "movedVec: "; movedVec.print(); // Использование оператора присваивания перемещением Vector3D anotherVec; anotherVec = std::move(movedVec); std::cout << "After move assignment operator:" << std::endl; std::cout << "anotherVec: "; anotherVec.print(); } // Демонстрация unique_ptr void demonstrateUniquePtr() { std::cout << "\n=== Demonstration of std::unique_ptr ===" << std::endl; // Создание unique_ptr std::unique_ptr<Vector3D> uniqueVec = std::make_unique<Vector3D>(4.0, 5.0, 6.0); std::cout << "Unique_ptr content: "; uniqueVec->print(); // Перемещение unique_ptr std::unique_ptr<Vector3D> movedUnique = std::move(uniqueVec); std::cout << "After moving unique_ptr:" << std::endl; if (!uniqueVec) { std::cout << "Original unique_ptr is now empty" << std::endl; } if (movedUnique) { std::cout << "Moved unique_ptr: "; movedUnique->print(); } // Использование unique_ptr в контейнере std::vector<std::unique_ptr<Vector3D>> vectors; vectors.push_back(std::make_unique<Vector3D>(1.0, 0.0, 0.0)); vectors.push_back(std::make_unique<Vector3D>(0.0, 1.0, 0.0)); vectors.push_back(std::make_unique<Vector3D>(0.0, 0.0, 1.0)); std::cout << "\nVectors in container:" << std::endl; for (const auto& vec : vectors) { vec->print(); } } // Демонстрация shared_ptr void demonstrateSharedPtr() { std::cout << "\n=== Demonstration of std::shared_ptr ===" << std::endl; // Создание shared_ptr auto sharedVec1 = createSharedVector(7.0, 8.0, 9.0); printVectorInfo(sharedVec1); // Создание копий shared_ptr auto sharedVec2 = sharedVec1; // Reference count increases auto sharedVec3 = sharedVec2; // Reference count increases std::cout << "\nAfter creating copies:" << std::endl; printVectorInfo(sharedVec1); // Использование функции, принимающей shared_ptr normalizeVector(sharedVec1); // Показываем, что все shared_ptr ссылаются на один объект std::cout << "\nModification through sharedVec2:" << std::endl; sharedVec2->setX(10.0); printVectorInfo(sharedVec1); printVectorInfo(sharedVec2); printVectorInfo(sharedVec3); // Освобождение ссылок sharedVec2.reset(); sharedVec3.reset(); std::cout << "\nAfter resetting sharedVec2 and sharedVec3:" << std::endl; printVectorInfo(sharedVec1); } // Демонстрация комбинированного использования void demonstrateCombinedUsage() { std::cout << "\n=== Combined Usage Demonstration ===" << std::endl; // Создание вектора с помощью unique_ptr std::unique_ptr<Vector3D> uniqueBase = std::make_unique<Vector3D>(2.0, 3.0, 4.0); // Преобразование unique_ptr в shared_ptr (передача владения) std::shared_ptr<Vector3D> sharedDerived = std::move(uniqueBase); std::cout << "After converting unique_ptr to shared_ptr:" << std::endl; if (!uniqueBase) { std::cout << "unique_ptr is released" << std::endl; } printVectorInfo(sharedDerived); // Создание нескольких shared_ptr на один объект auto sharedCopy1 = sharedDerived; auto sharedCopy2 = sharedDerived; std::cout << "\nCreated copies of shared_ptr:" << std::endl; std::cout << "Total reference count: " << sharedDerived.use_count() << std::endl; } int main() { std::cout << "Demonstration Program for Move Semantics and Smart Pointers\n" << "============================================================" << std::endl; // Базовые операции с Vector3D std::cout << "\n=== Basic Operations ===" << std::endl; Vector3D v1(1.0, 2.0, 3.0); Vector3D v2(4.0, 5.0, 6.0); std::cout << "v1: "; v1.print(); std::cout << "v2: "; v2.print(); Vector3D v3 = v1 + v2; std::cout << "v1 + v2 = "; v3.print(); std::cout << "Length of v1: " << v1.length() << std::endl; std::cout << "Dot product of v1 and v2: " << v1.dotProduct(v2) << std::endl; // Демонстрация различных аспектов demonstrateMoveSemantics(); demonstrateUniquePtr(); demonstrateSharedPtr(); demonstrateCombinedUsage(); std::cout << "\n=== Program Completion ===" << std::endl; std::cout << "Number of Vector3D objects at the end: " << Vector3D::getObjectCount() << std::endl; return 0; }