Download presentation
Presentation is loading. Please wait.
Published byDewi Irawan Modified over 6 years ago
1
CMPE 135: Object-Oriented Analysis and Design November 27 Class Meeting
Department of Computer Engineering San Jose State University Fall 2018 Instructor: Ron Mak
2
The auto Keyword In a declaration of a variable that is also being initialized, the compiler can infer the type of the variable from the initialization expression. type inference, AKA type determination Use auto instead of a complicated type name. Examples: Instead of: Use: vector<int>::iterator current = a_container.begin(); map<string, DistanceToCity>::iterator p = cities.lower_bound(new_city.get_name()); auto current = a_container.begin(); auto p = cities.lower_bound(new_city.get_name());
3
The decltype Pseudo-Function
Takes a variable as an argument. Returns the type associated with the variable. Create another variable with the same type. Ensure that two variables have the same type. Example: map<string, int> m; // Type inferred to be map<string, int>::iterator auto start_point = m.begin(); // Must also be a map<string, int>::iterator decltype(start_point) it;
4
Problems with a Standard (“Raw”) Pointer
A raw pointer’s declaration doesn’t indicate whether: It “owns” the object that points to (should you destroy the object?) It points to a single object or to an array (should you call delete or delete[]?) You should call a dedicated destruction function. Will you perform the destruction exactly once? Memory leak if you never destroy the object. Segmentation fault if you destroy the object more than once.
5
Raw Pointers vs. Smart Pointers
Raw pointers are powerful but dangerous. Use a smart pointer instead. Unique pointer Has exclusive ownership of the object it points to. The ownership can be transferred. The object is automatically destroyed when the pointer goes out of scope. Shared pointer Multiple pointers can point to the same object. The object is destroyed only once.
6
Unique Pointer Exclusive ownership of the object.
Only one unique pointer at a time can point to a particular object. Ownership can be transferred to another unique pointer. The source pointer is automatically set to null. When the owning pointer is set to point to another object, or goes out of scope, the object it points to is automatically destroyed.
7
Unique Pointer Example
We’ll use our old Birthday class. The default constructor, constructor, and destructor each writes a message when it’s called. TestUnique.cpp int main() { Birthday *raw_ptr = new Birthday(2001, 1, 1); unique_ptr<Birthday> uniq_ptr1(new Birthday(2002, 2, 2)); unique_ptr<Birthday> uniq_ptr2(nullptr); cout << "*raw_ptr = " << *raw_ptr << endl; cout << "*uniq_ptr1 = " << *uniq_ptr1 << endl; ... *** Constructor called for 1/1/2001 *** Constructor called for 2/2/2002 *raw_ptr = 1/1/2001 *uniq_ptr1 = 2/2/2002
8
Unique Pointer Example, cont’d
“Move” the object from one pointer to another. Can’t copy, because of exclusive ownership. The source pointer is automatically set to nullptr. uniq_ptr2 = move(uniq_ptr1); if (uniq_ptr1 == nullptr) cout << "uniq_ptr1 is now null" << endl; cout << "*uniq_ptr2 = " << *uniq_ptr2 << endl; uniq_ptr1 is now null *uniq_ptr2 = 2/2/2002
9
Unique Pointer Example, cont’d
You can associate a custom destructor with a unique pointer. In this example, we use a lambda expression. Also note the use of auto and decltype. auto delete_birthday = [] (Birthday *ptr) { cout << "Deleting " << *ptr << endl; delete ptr; }; unique_ptr<Birthday, decltype(delete_birthday)> uniq_ptr3(new Birthday(2003, 3, 3), delete_birthday); cout << "*uniq_ptr3 = " << *uniq_ptr3 << endl; *** Constructor called for 3/3/2003 *uniq_ptr3 = 3/3/2003
10
Unique Pointer Example, cont’d
Use the reset member function to change the value of a unique pointer. unique_ptr<Birthday> uniq_ptr4(nullptr); uniq_ptr4.reset(new Birthday(2004, 4, 4)); cout << "*uniq_ptr4 = " << *uniq_ptr4 << endl; uniq_ptr4.reset(new Birthday(2005, 5, 5)); *** Constructor called for 4/4/2004 *uniq_ptr4 = 4/4/2004 *** Constructor called for 5/5/2005 *** Destructor called for 4/4/2004 *uniq_ptr4 = 5/5/2005
11
Unique Pointer Example, cont’d
When a unique smart pointer goes out of scope, the object it points to is automatically removed from memory. return 0; } *** Destructor called for 5/5/2005 Deleting 3/3/2003 *** Destructor called for 3/3/2003 *** Destructor called for 2/2/2002 Memory leak: Birthday *raw_ptr = new Birthday(2001, 1, 1);
12
Shared Pointer Multiple shared smart pointers can point at the same time to a single object. Shared ownership resource management. When the last pointer no longer points to the object, the object is automatically destroyed. It is destroyed only once. Uses reference counts.
13
Shared Pointer Example
int main() { shared_ptr<Birthday> shar_ptr1(new Birthday(2001, 1, 1)); shared_ptr<Birthday> shar_ptr2(shar_ptr1); cout << "*shar_ptr1 = " << *shar_ptr1 << endl; { cout << endl << "Entering new scope!" << endl; shared_ptr<Birthday> shar_ptr3(shar_ptr2); shared_ptr<Birthday> shar_ptr4(shar_ptr3); cout << "*shar_ptr3 = " << *shar_ptr3 << endl; cout << "*shar_ptr4 = " << *shar_ptr4 << endl; cout << "Exiting scope!" << endl << endl; } cout << "*shar_ptr2 = " << *shar_ptr2 << endl; return 0; } TestShared.cpp Nested scope
14
Shared Pointer Example, cont’d
int main() { shared_ptr<Birthday> shar_ptr1(new Birthday(2001, 1, 1)); shared_ptr<Birthday> shar_ptr2(shar_ptr1); cout << "*shar_ptr1 = " << *shar_ptr1 << endl; *** Constructor called for 1/1/2001 *shar_ptr1 = 1/1/2001
15
Shared Pointer Example, cont’d
{ cout << endl << "Entering new scope!" << endl; shared_ptr<Birthday> shar_ptr3(shar_ptr2); shared_ptr<Birthday> shar_ptr4(shar_ptr3); cout << "*shar_ptr3 = " << *shar_ptr3 << endl; cout << "*shar_ptr4 = " << *shar_ptr4 << endl; cout << "Exiting scope!" << endl << endl; } Entering new scope! *shar_ptr3 = 1/1/2001 *shar_ptr4 = 1/1/2001 Exiting scope! Nothing special happens when we exit the scope. shar_ptr3 and shar_ptr4 go out of scope.
16
Shared Pointer Example, cont’d
cout << "*shar_ptr2 = " << *shar_ptr2 << endl; return 0; } *shar_ptr2 = 1/1/2001 *** Destructor called for 1/1/2001 The destructor is automatically called once the last pointer to the object goes out of scope.
17
Shared Pointer Implementation
Effective Modern C++ by Scott Meyers O’Reilly, 2015 ISBN
18
Introduction to Move Semantics
As we’ve seen, excessive calls to the copy constructor can hurt performance. In a move from source to target, instead of copying a resource, the target “steals” the resource from the source. The source contains a pointer to an object. The target gets a copy of the pointer. The source’s pointer is set to null. Therefore, the target has taken ownership of the object.
19
Move Semantics Example
Message.h class Message { private: int length; char *text; public: Message() : length(0), text(nullptr) {} Message(char *t) : length(strlen(t)), text(t) {} Message(const Message& other) { length = other.length; text = new char[length + 1]; strcpy(text, other.text); } Copy constructor
20
Move Semantics Example, cont’d
Message(Message&& other) { cout << "*** Move constructor called!" << endl; length = other.length; text = other.text; other.length = 0; other.text = nullptr; } virtual ~Message() if (length > 0) delete[] text; length = 0; text = nullptr; Move constructor Steal the source’s object without copying it.
21
Move Semantics Example, cont’d
Message& operator =(const Message& other) { if (this != &other) { if (length > 0) delete[] text; length = other.length; text = new char[length + 1]; strcpy(text, other.text); } return *this; } friend ostream& operator <<(ostream& outs, const Message& msg) cout << msg.length << ":"; if (msg.text != nullptr) cout << msg.text; else cout << "<empty>"; return outs; };
22
Move Semantics Example, cont’d
TestMove.cpp #include "Message.h" int main() { char h[16], w[16]; strcpy(h, "hello"); strcpy(w, "world"); cout << "h = " << h << endl; cout << "w = " << w << endl; cout << endl << "Default constructor:" << endl; Message msg; cout << "msg = " << msg << endl; h = hello w = world Default constructor: msg = 0:<empty>
23
Move Semantics Example, cont’d
cout << endl << "Regular constructor:" << endl; Message msg_h1(h); Message msg_w1(w); cout << "msg_h1 = " << msg_h1 << endl; cout << "msg_w1 = " << msg_w1 << endl; cout << endl << "Copy constructor:" << endl; Message msg_h2(msg_h1); cout << "msg_h2 = " << msg_h2 << endl; Regular constructor: msg_h1 = 5:hello msg_w1 = 5:world Copy constructor: msg_h2 = 5:hello
24
Move Semantics Example, cont’d
cout << endl << "Overloaded assignment operator:" << endl; Message msg_w2; msg_w2 = msg_w1; cout << "msg_w1 = " << msg_w1 << endl; cout << "msg_w2 = " << msg_w2 << endl; cout << endl << "Move constructor:" << endl; Message msg_h3(move(msg_h1)); cout << "msg_h1 = " << msg_h1 << endl; cout << "msg_h3 = " << msg_h3 << endl; return 0; } Overloaded assignment operator: msg_w1 = 5:world msg_w2 = 5:world Move constructor: *** Move constructor called! msg_h1 = 0:<empty> msg_h3 = 5:hello
25
Introduction to Move Semantics, cont’d
Move semantics involve lvalues and rvalues. An lvalue is an object. So called because it can appear on the left side of an assignment statement (i.e., it can be an assignment target). An rvalue is a temporary value or a value not associated with an object. So called because it can be a value that’s calculated on the right side of an assignment statement.
26
Introduction to Move Semantics, cont’d
When an rvalue is assigned in an assignment statement, or when an rvalue is passed by value to a function, a copy is made of the value. But it is wasteful to copy a temporary value. That value normally is about to disappear. Move semantics allows us to use that temporary value by taking ownership of its resources. Reduce the amount of runtime copying.
27
Presentation Schedule
Tuesday, Dec. 4 #include "TeamName.h" CraZ CoderZ Moon Cakes No Name 2 Thursday, Dec. 6 Titans main.cpp One with the Object Jam
28
Team Project Presentations
Open ended! Web app Desktop app GUI-based Command-line-based During each presentation, the rest of the class will fill out a survey. The presentation part of your project grade will be significantly based on the survey results. Projects and reports due Monday, Dec. 10
29
Survey Questions (First Draft)
Overview The application Architecture and technologies Design Use of design principles Use of design patterns Demo How did it go Q&A
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.