命名空间
变体
操作

std::shared_ptr<T>::operator*, std::shared_ptr<T>::operator->

来自 cppreference.cn
< cpp‎ | 内存‎ | shared ptr
 
 
内存管理库
(仅作说明*)
未初始化内存算法
(C++17)
(C++17)
(C++17)
受约束的未初始化
内存算法
C 库

分配器
内存资源
垃圾回收支持
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
未初始化存储
(直到 C++20*)
(直到 C++20*)
显式生命周期管理
 
 
T& operator*() const noexcept;
(1) (C++11 起)
T* operator->() const noexcept;
(2) (C++11 起)

解引用所存储的指针。若所存储的指针为空,则行为未定义。

目录

[编辑] 参数

(无)

[编辑] 返回值

1) 解引用所存储指针的结果,即 *get()
2) 所存储的指针,即 get()

[编辑] 注解

T数组类型或(可能被 cv 限定的)(C++17 起) void 时,是否声明函数 (1) 是未指定的。若其被声明,则其返回类型是未指定的,除了该函数的声明(虽然不必须是定义)必须为良构的。这使得实例化 std::shared_ptr<void> 成为可能。

T 为数组类型时,是否声明函数 (2) 是未指定的。若其被声明,则其返回类型是未指定的,除了该函数的声明必须为良构的。

(C++17 起)

[编辑] 示例

#include <iostream>
#include <memory>
 
struct Foo
{
    Foo(int in) : a(in) {}
    void print() const
    {
        std::cout << "a = " << a << '\n';
    }
    int a;
};
 
int main()
{
    auto ptr = std::make_shared<Foo>(10);
    ptr->print();
    (*ptr).print();
}

输出

a = 10
a = 10

[编辑] 参阅

返回存储的指针
(公开成员函数) [编辑]