Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Methods of a Unique Pointer | Unique Pointers
C++ Smart Pointers

bookMethods of a Unique Pointer

Since std::unique_ptr is a class template it provides various methods to manage itself safely.

main.cpp

main.cpp

copy
12345678910111213141516171819202122232425262728293031323334353637383940414243
#include <iostream> #include <memory> struct Sample { Sample(int v) : value(v) {} int value; void GetValue() { std::cout << "Value: " << value << std::endl; } }; int main() { // Creating a `unique_ptr` std::unique_ptr<Sample> p_first = std::make_unique<Sample>(10); // Using `get()` to retrieve the raw pointer Sample* p_raw = p_first.get(); std::cout << "Raw pointer value: " << p_raw->value << std::endl; // Using `release()` to transfer ownership Sample* p_released = p_first.release(); if (!p_first) std::cout << "p_first is now empty after release.\n"; // Manually delete the released pointer to avoid memory leak delete p_released; // Resetting the `unique_ptr` p_first.reset(new Sample(20)); p_first->GetValue(); // Using `swap()` with another `unique_ptr` std::unique_ptr<Sample> p_second = std::make_unique<Sample>(30); p_first.swap(p_second); p_first->GetValue(); // Now `p_first` owns `p_second` object // Dereferencing the pointer std::cout << "Dereferencing p_first: " << (*p_first).value << std::endl; // Accessing members using `->` operator p_first->GetValue(); }
question mark

What does the reset() function do when called without a parameter?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 5

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Awesome!

Completion rate improved to 5.56

bookMethods of a Unique Pointer

Stryg for at vise menuen

Since std::unique_ptr is a class template it provides various methods to manage itself safely.

main.cpp

main.cpp

copy
12345678910111213141516171819202122232425262728293031323334353637383940414243
#include <iostream> #include <memory> struct Sample { Sample(int v) : value(v) {} int value; void GetValue() { std::cout << "Value: " << value << std::endl; } }; int main() { // Creating a `unique_ptr` std::unique_ptr<Sample> p_first = std::make_unique<Sample>(10); // Using `get()` to retrieve the raw pointer Sample* p_raw = p_first.get(); std::cout << "Raw pointer value: " << p_raw->value << std::endl; // Using `release()` to transfer ownership Sample* p_released = p_first.release(); if (!p_first) std::cout << "p_first is now empty after release.\n"; // Manually delete the released pointer to avoid memory leak delete p_released; // Resetting the `unique_ptr` p_first.reset(new Sample(20)); p_first->GetValue(); // Using `swap()` with another `unique_ptr` std::unique_ptr<Sample> p_second = std::make_unique<Sample>(30); p_first.swap(p_second); p_first->GetValue(); // Now `p_first` owns `p_second` object // Dereferencing the pointer std::cout << "Dereferencing p_first: " << (*p_first).value << std::endl; // Accessing members using `->` operator p_first->GetValue(); }
question mark

What does the reset() function do when called without a parameter?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 5
some-alt