Namespaces
Variants

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

From cppreference.net
Memory management library
( exposition only* )
Allocators
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Memory resources
Uninitialized storage (until C++20)
( until C++20* )
( until C++20* )
( until C++20* )

Garbage collector support (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
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) 是未指定的。若该函数被声明,其返回类型也是未指定的,但该函数的声明必须保持良构。

(since 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

参见

返回存储的指针
(公开成员函数)