Presentation is loading. Please wait.

Presentation is loading. Please wait.

Practical Session 4 Rule of 5 R-value reference Unique pointer

Similar presentations


Presentation on theme: "Practical Session 4 Rule of 5 R-value reference Unique pointer"— Presentation transcript:

1 Practical Session 4 Rule of 5 R-value reference Unique pointer
Virtual destructor Namespace

2 Rule of 5 destructor copy constructor move constructor
copy assignment operator move assignment operator

3 Rule of 5 – h file. class charArray { private: char *cstring;
int cstringlen; void copy(const char *other_cstring, const int &other_cstringlen); void clear(); public: const char* toString() const; charArray(const char *arg); // Constructor virtual ~charArray(); // Destructor charArray(const charArray &other); // Copy Constructor charArray(charArray &&other); // Move Constructor charArray& operator=(const charArray &other); // Copy Assignment charArray& operator=(charArray &&other); // Move Assignment };

4 The original three charArray::copy(const char *other_cstring, const int &other_cstringlen) { cstringlen = other_cstringlen; cstring = new char[cstringlen + 1]; // allocate strcpy_s(cstring, cstringlen + 1, other_cstring); // populate } charArray::charArray(const char *arg) { copy(arg, std::strlen(arg)); charArray::charArray(const charArray &other) { copy(other.cstring, other.cstringlen); charArray& charArray::operator=(const charArray &other) { if (this != &other) { clear(); return *this;

5 The additional two 10 charArray& charArray::operator=(charArray &&other){ if (this != &other){ clear(); cstringlen = other.cstringlen; cstring = other.cstring; other.cstring = nullptr; other.cstringlen = 0; } return *this; charArray::charArray(charArray&& other) : cstring(other.cstring), cstringlen(other.cstringlen) { other.cstring = nullptr; other.cstringlen = 0; }

6 r-value reference usage:
Containers, such as vectors cannot hold references. Which means, each time we push_back an element, copy constructor is executed. c++11 solved this issue by creating r-value references: Now values are moved instead of being copied: void push_back( const T& value ); void push_back( T&& value ); Value of “s” after line 4? holds "" std::vector<std::string> numbers; numbers.push_back("abc"); std::string s = "def"; //def is r-value numbers.push_back(std::move(s)); //this makes “s” r-value.

7 std::unique_ptr Same as std::shared_ptr with one difference:
Holds an object uniquely Which means no other pointer may point to it. Object pointed by unique_ptr is deleted in these cases: std::unique_ptr is destroyed (exiting its scope) std::unique_ptr::reset is called. Note: Regular pointers can point to the object unique_ptr points at. There is no synchronization between old ways and new ways in c++. The programmer’s job is to ensure correctness in case of usage multiple pointer types.

8 Std::unique_ptr example
16 17 18 19 20 class Foo { public: Foo() { std::cout << "Foo::Foo\n"; } ~Foo() { std::cout << "Foo::~Foo\n"; } void bar() { std::cout << "Foo::bar\n"; } }; void f(const Foo &) { std::cout << "f(const Foo&)\n"; } void main() { std::unique_ptr<Foo> p1(new Foo()); // p1 owns Foo if (p1) p1->bar(); { std::unique_ptr<Foo> p2(std::move(p1)); // now p2 owns Foo f(*p2); p1 = std::move(p2); // ownership returns to p1 std::cout << "destroying p2...\n"; if (p1) p1->bar(); // Foo instance is destroyed when p1 goes out of scope

9 Rule of 0 Write your classes in a way that the compiler-provided destructor, copy/move ctor/assignment automatically are correct (either defaulted or deleted). Which means you do not need to implement them. All resource management should be put in the hand of library classes and corresponding members. new/delete are verboten! Don't use plain pointers. Exception: initializing a unique_ptr until C++14. Polymorphic types that need to be kept on the heap should always be instantiated via make_shared(). Rely on library experts to provide resource management for you.

10 Virtual Destructor Destructor: Example: Person *p = new Boy()
Delete p; //this will execute Person’s destructor only. virtual ~person(){} Delete p; //this will execute Boy’s destructor then Person’s destructor.

11 Namespaces namespace mine { int i, j;} namespace yours { int i, k, n;} int global_name_known_to_linker; static int global_to_this_file_but_not_to_linker; int main(void) { int n = 1; mine::i = 2; yours::i = 3; yours::n = 4; using namespace mine; j = 5; using namespace yours; k = 6; // i = 7; error, ambiguous now that 'using' both namespaces n = 8; // local 'n' not overridden or ambiguous cout << mine::i << yours::i << j << k << n << yours::n << '\n'; return 0; }


Download ppt "Practical Session 4 Rule of 5 R-value reference Unique pointer"

Similar presentations


Ads by Google