Antworten
Content |
Hier sind die Antworten zu den einzelnen Fragen:
-
Das erste Beispiel:
#include <iostream> class Vector2D { public: Vector2D() : x(0), y(0) { std::cout << "X: default constructor" << std::endl; } Vector2D(const Vector2D& other) = delete; Vector2D& operator=(const Vector2D& other) { std::cout << "X: assignment operator" << std::endl; x = other.x; y = other.y; return *this; } double x, y; }; int main() { Vector2D a; Vector2D b = a; }
theon$ g++ -Wall -c case06.cpp case06.cpp: In function 'int main()': case06.cpp:19:17: error: use of deleted function 'Vector2D::Vector2D(const Vector2D&)' Vector2D b = a; ^ case06.cpp:8:7: note: declared here Vector2D(const Vector2D& other) = delete; ^~~~~~~~ theon$
Da der Kopierkonstruktor entfernt wurde, können Objekte dieses Typs nicht mehr kopier-konstruiert werden. Zuweisungen sind allerdings noch möglich:
theon$ diff -U 2 case06.cpp case06-fixed.cpp --- case06.cpp Thu May 3 11:58:32 2018 +++ case06-fixed.cpp Thu May 3 13:54:20 2018 @@ -17,4 +17,4 @@ int main() { Vector2D a; - Vector2D b = a; + Vector2D b; b = a; } theon$ g++ -Wall -o case06-fixed case06-fixed.cpp theon$ ./case06-fixed X: default constructor X: default constructor X: assignment operator theon$
-
Beim zweiten Problem kopieren die vom Übersetzer erzeugten Methoden (Kopierkonstruktor und Zuweisungsoperator) nur den Zeiger, d.h. alle Variablen haben den gleichen Zeiger, den sie alle beim Abbau freigeben.
heim$ diff -U 2 data-on-heap.cpp data-on-heap-fixed.cpp --- data-on-heap.cpp 2018-05-03 14:10:01.762696058 +0200 +++ data-on-heap-fixed.cpp 2018-05-03 14:10:24.285093797 +0200 @@ -9,8 +9,18 @@ std::cout << "X: constructor with i = " << i << std::endl; } + X(const X& other) : data(new int{*other.data}) { + std::cout << "X: copy constructor with *other.data = " + << *other.data << std::endl; + } ~X() { std::cout << "X: destructor with *data = " << *data << std::endl; delete data; } + X& operator=(const X& other) { + std::cout << "X: assignment operator with *other.data = " + << *other.data << std::endl; + *data = *other.data; + return *this; + } private: int* data; heim$ g++-7.2 -Wall -o data-on-heap-fixed data-on-heap-fixed.cpp heim$ ./data-on-heap-fixed X: constructor with i = 42 X: copy constructor with *other.data = 42 X: default constructor X: assignment operator with *other.data = 42 X: destructor with *data = 42 X: destructor with *data = 42 X: destructor with *data = 42 heim$ valgrind ./data-on-heap-fixed ==15006== Memcheck, a memory error detector ==15006== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al. ==15006== Using Valgrind-3.10.0 and LibVEX; rerun with -h for copyright info ==15006== Command: ./data-on-heap-fixed ==15006== X: constructor with i = 42 X: copy constructor with *other.data = 42 X: default constructor X: assignment operator with *other.data = 42 X: destructor with *data = 42 X: destructor with *data = 42 X: destructor with *data = 42 ==15006== ==15006== HEAP SUMMARY: ==15006== in use at exit: 72,704 bytes in 1 blocks ==15006== total heap usage: 4 allocs, 3 frees, 72,716 bytes allocated ==15006== ==15006== LEAK SUMMARY: ==15006== definitely lost: 0 bytes in 0 blocks ==15006== indirectly lost: 0 bytes in 0 blocks ==15006== possibly lost: 0 bytes in 0 blocks ==15006== still reachable: 72,704 bytes in 1 blocks ==15006== suppressed: 0 bytes in 0 blocks ==15006== Rerun with --leak-check=full to see details of leaked memory ==15006== ==15006== For counts of detected and suppressed errors, rerun with: -v ==15006== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) heim$