Use the overloaded -> and * operators to access the object. (Some utility functions or smart pointer constructors do this for you.) Pass a raw pointer to a new-ed object in the smart pointer constructor. In the type parameter, specify the pointed-to type of the encapsulated pointer. (Do not use the new or malloc expression on the smart pointer itself.) The example demonstrates the following essential steps for using smart pointers.ĭeclare the smart pointer as an automatic (local) variable. Void ProcessLargeObject(const LargeObject& lo) //pLarge is deleted automatically when function block goes out of scope. The following example shows how a unique_ptr smart pointer type from the C++ Standard Library could be used to encapsulate a pointer to a large object. The difference is that no separate garbage collector runs in the background memory is managed through the standard C++ scoping rules so that the runtime environment is faster and more efficient.Īlways create smart pointers on a separate line of code, never in a parameter list, so that a subtle resource leak won't occur due to certain parameter list allocation rules. The C++ smart pointer idiom resembles object creation in languages such as C#: you create the object and then let the system take care of deleting it at the correct time.
![smart notebook 11 tutorial smart notebook 11 tutorial](https://image.slidesharecdn.com/1-pptrevisedtutorialforthesmartboardsoftwaredownloadandmaincapabilities-100414091620-phpapp02/95/1-ppt-revised-tutorial-for-the-smart-board-software-download-and-main-capabilities-11-728.jpg)
The smart pointer destructor contains the call to delete, and because the smart pointer is declared on the stack, its destructor is invoked when the smart pointer goes out of scope, even if an exception is thrown somewhere further up the stack.Īccess the encapsulated pointer by using the familiar pointer operators, -> and *, which the smart pointer class overloads to return the encapsulated raw pointer. This means that the smart pointer is responsible for deleting the memory that the raw pointer specifies. After the smart pointer is initialized, it owns the raw pointer. } // song2 is deleted automatically here.Īs shown in the example, a smart pointer is a class template that you declare on the stack, and initialize by using a raw pointer that points to a heap-allocated object. Unique_ptr song2(new Song(L"Nothing on You", L"Bruno Mars")) Declare a smart pointer on stack and pass it the raw pointer. Song* pSong = new Song(L"Nothing on You", L"Bruno Mars")
![smart notebook 11 tutorial smart notebook 11 tutorial](https://www.gizbot.com/files/2012/01/smart-notebook-11-software.jpg)
![smart notebook 11 tutorial smart notebook 11 tutorial](https://img.youtube.com/vi/xt6_YlHg-dw/mqdefault.jpg)
Using a raw pointer - not recommended. The following example compares a raw pointer declaration to a smart pointer declaration.
#Smart notebook 11 tutorial code
In modern C++, raw pointers are only used in small code blocks of limited scope, loops, or helper functions where performance is critical and there is no chance of confusion about ownership. In most cases, when you initialize a raw pointer or resource handle to point to an actual resource, pass the pointer to a smart pointer immediately.
#Smart notebook 11 tutorial free
In practical terms, the main principle of RAII is to give ownership of any heap-allocated resource-for example, dynamically-allocated memory or system object handles-to a stack-allocated object whose destructor contains the code to delete or free the resource and also any associated cleanup code. The main goal of this idiom is to ensure that resource acquisition occurs at the same time that the object is initialized, so that all resources for the object are created and made ready in one line of code. They are crucial to the RAII or Resource Acquisition Is Initialization programming idiom. Smart pointers are defined in the std namespace in the header file. In modern C++ programming, the Standard Library includes smart pointers, which are used to help ensure that programs are free of memory and resource leaks and are exception-safe.