diff options
3 files changed, 105 insertions, 0 deletions
| -rw-r--r-- | src/teaching/oop-2025/index.du | 3 | ||||
| -rw-r--r-- | src/teaching/oop-2025/oop-notes/RuleOfFive.cpp | 54 | ||||
| -rw-r--r-- | src/teaching/oop-2025/oop-notes/index.du | 48 |
diff --git a/src/teaching/oop-2025/index.du b/src/teaching/oop-2025/index.du index 71c87e8..fa6b58d 100644 --- a/src/teaching/oop-2025/index.du +++ b/src/teaching/oop-2025/index.du @@ -12,6 +12,9 @@ | [url https://github.com/18ivan18/ObjectOrientedProgramming2026 Материали от семинари] | [url https://learn.fmi.uni-sofia.bg/course/view.php?id=12155 Курс в мудъл] +[=Допълнителни ресурси:=] +[url ./oop-notes Бележки по ООП при C++] + :table_3 |= № |= Дата diff --git a/src/teaching/oop-2025/oop-notes/RuleOfFive.cpp b/src/teaching/oop-2025/oop-notes/RuleOfFive.cpp new file mode 100644 index 0000000..c29afe2 --- /dev/null +++ b/src/teaching/oop-2025/oop-notes/RuleOfFive.cpp @@ -0,0 +1,54 @@ +class X { + Y* elements; + unsigned elementsCount; + + void free() { + delete[] elements; + } + void copyFrom(const X& other) { + elementsCount = other.elementsCount; + + elements = new Y[elementsCount]; + for (unsigned i = 0; i < elementsCount; ++i) + elements[i] = other.elements[i]; + } + +public: + X() { + elements = nullptr; + elementsCount = 0; + } + ~X() { + free(); + } + X(const X& other) { + copyFrom(other); + } + X& operator=(const X& other) { + if (this != &other) { + free(); + copyFrom(other); + } + return *this; + } + + X(X&& other) { + elements = other.elements; + elementsCount = other.elementsCount; + + other.elements = nullptr; + other.elementsCount = 0; + } + X& operator=(X&& other) { + if (this != &other) { + free(); + + elements = other.elements; + elementsCount = other.elementsCount; + + other.elements = nullptr; + other.elementsCount = 0; + } + return *this; + } +}; diff --git a/src/teaching/oop-2025/oop-notes/index.du b/src/teaching/oop-2025/oop-notes/index.du new file mode 100644 index 0000000..c3641fa --- /dev/null +++ b/src/teaching/oop-2025/oop-notes/index.du @@ -0,0 +1,48 @@ +# Бележки по ООП при C++ - ООП, КН, 2025-2026 + +'define expected-reading 2 min +'define created 18 March 2026 +'define edited 18 March 2026 +[$pagenav] + +Този документ съдържа редица полезна информация и детайли относно обектно-ориентираното програмиране при C++. + +## Голяма петица формат + +```c++ +'include ./RuleOfFive.cpp + +## [`&&A`], rvalue и rvalue reference + +[`lvalue`] е всяка стойност, която има "име". +Това са аргументи на функции, променливи, ... + +[`rvalue`] е всяка стойност за която нямаме "име". +Най-често срещаните са върнатите стойноти от функция/оператор. +Тези стойности са временни и идеята е че ще бъдат изтрити (и на практика, данните им ще бъдат копирани/пренесени в нещо с име, [`lvalue`]). + +Можем да правим референции върху [`rvalue`]: +```c++ +A&& rvalueRef = functionWhichReturnsA(); + +Обаче, тези референции дават (временно) име към [`rvalue`]-то. +Това означава, че те самите стават [`lvalue`]. + +Поради тази прична не можем да имаме повече от една [`rvalue`] референция към едно [`rvalue`]: +```c++ +A&& ref1 = functionWhichReturnsA(); +A&& ref2 = ref1; // Грешка! + +И не можем да подаваме аргумент [`rvalue`] на друга функция: +```c++ +void foo(A&& ref1) { + ... +} +void bar(A&& ref2) { + foo(ref2); // Грешка! +} + +\n\n +[*Съпътстващи ресурси:*] +.bulleted +- [url https://web.archive.org/web/20200627194253/https://thbecker.net/articles/rvalue_references/section_05.html https://thbecker.net/articles/rvalue_references/section_05.html] |