Home > Backend Development > C++ > body text

C++ Smart Pointers: Advanced Usage and Considerations

王林
Release: 2024-05-09 17:06:05
Original
246 people have browsed it

C++ 智能指针:高级用法和注意事项

C Smart Pointers: Advanced Usage and Precautions

Advanced Usage:

1. Custom smart pointers:
You can create your own smart pointers, inherit from std::unique_ptr or std::shared_ptr, and customize the behavior for specific needs. .

class CustomPtr : public std::unique_ptr {
public:
    CustomPtr(int* ptr) : std::unique_ptr(ptr) {}
    ~CustomPtr() { std::cout << "CustomPtr destroyed" << std::endl; }
};
Copy after login

2. Convert smart pointers:
You can use the std::make_unique and std::make_shared functions to easily Convert between types of smart pointers.

std::unique_ptr up = std::make_unique(42);
std::shared_ptr sp = std::make_shared(*up);
Copy after login

3. Circular Reference:
When two smart pointers refer to each other, a circular reference may be created. You can change the type of one of the smart pointers to break the cycle.

struct Node {
    std::unique_ptr next;
    std::shared_ptr prev;
};
Copy after login

Notes:

1. Memory leak:
Forgetting to wrap resources with smart pointers can lead to memory leaks. Make sure to always use smart pointers to manage dynamically allocated memory.

2. Dangling pointers:
Smart pointers may point to deleted objects, resulting in dangling pointers. Avoid checking the validity of smart pointers before using them.

3. Self-looping:
Smart pointers can point to themselves, causing self-looping. Avoid allocating the same object in the constructor of a smart pointer.

4. Performance overhead:
Smart pointers have performance overhead than raw pointers. Use them when needed, such as in complex data structures or when accessing resources across multiple threads.

Practical example:

Consider the following example, which shows how shared pointers prevent dangling pointers:

class Widget {
public:
    std::shared_ptr createChild() {
        return std::make_shared();
    }
};

int main() {
    std::shared_ptr parent = std::make_shared();
    std::shared_ptr child = parent->createChild();
    
    // ... 代码块 1,父对象 parent 被删除 ...
    
    // 代码块 2,child 仍然有效
    child->doSomething();
    
    return 0;
}
Copy after login

In this example, even if the parent objectparent is removed, child in block 2 is still valid because std::shared_ptr maintains shared ownership of the object.

The above is the detailed content of C++ Smart Pointers: Advanced Usage and Considerations. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!