std::shared_ptr<T>::operator*, std::shared_ptr<T>::operator->
来自cppreference.com
<tbody>
</tbody>
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> 可以实例化。
|
当 |
(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
参阅
| 返回存储的指针 (公开成员函数) |