Professional Documents
Culture Documents
1. Pointers
2. Dynamic Allocation
3. Exceptions
4. Copying, Assignment, and Destruction
5. Resource Acquisition is Initialization (RAII)
6. Smart Pointers
Module Overview
1. Pointers
2. Dynamic Allocation
3. Exceptions
4. Copying, Assignment, and Destruction
5. Resource Acquisition is Initialization (RAII)
6. Smart Pointers
Lesson 1: Pointers
void f()
{
int x{1};
int y{2};
}
… x y …
Lesson 1: Pointers
void f()
{
int x{1};
int y{2};
}
&x &y
… x y …
Lesson 1: Pointers
void f()
{
int x{1};
int y{2};
int* pointer_to_x{&x};
int* pointer_to_y{&y};
}
Lesson 1: Pointers
void f()
{
int x{1};
int* pointer_to_x{&x};
int y{*pointer_to_x};
}
Lesson 1: Pointers
void f()
{
int x{1};
int* pointer_to_x{&x};
*pointer_to_x = 2;
}
Lesson 1: Pointers
void f()
{
int x{1};
int y{2};
int* p{&x};
p = &y;
}
Lesson 1: Pointers
void f()
{
int p{nullptr};
}
DEMO
Pointers
Module Overview
1. Pointers
2. Dynamic Allocation
3. Exceptions
4. Copying, Assignment, and Destruction
5. Resource Acquisition is Initialization (RAII)
6. Smart Pointers
Lesson 2: Dynamic Allocation
void f()
{
int* p{new int{1}};
*p = 2;
delete p;
}
DEMO
Pointers
Module Overview
1. Pointers
2. Dynamic Allocation
3. Exceptions
4. Copying, Assignment, and Destruction
5. Resource Acquisition is Initialization (RAII)
6. Smart Pointers
Exceptions
try
{
// anything, including function calls
}
catch (const std::exception& e)
{
std::cout << "Exception caught: " << e.what();
}
DEMO
Exceptions
Module Overview
1. Pointers
2. Dynamic Allocation
3. Exceptions
4. Copying, Assignment, and Destruction
5. Resource Acquisition is Initialization (RAII)
6. Smart Pointers
Copying, Assignment, and Destruction
void f()
{
int x{1};
void f()
{
Rectangle x{3, 4};
void f()
{
int x{1}; // x comes into existence
int y{2}; // y comes into existence
} // y goes out of existence
// x goes out of existence
Destructor
1. Pointers
2. Dynamic Allocation
3. Exceptions
4. Copying, Assignment, and Destruction
5. Resource Acquisition is Initialization (RAII)
6. Smart Pointers
DEMO
Smart Pointers
©2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Office, Azure, System Center, Dynamics and other product names are or may be registered trademarks and/or trademarks in the
U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft
must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after
the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.