std::shared_ptr::get
public member function - 公开成员函数
Defined in header
- 定义于头文件
std::shared_ptr::get
element_type* get() const noexcept;
Get pointer.
获取指针。
Returns the stored pointer
.
返回存储的指针。
The stored pointer
points to the object the shared_ptr
object dereferences to, which is generally the same as its owned pointer
.
存储的指针指向 shared_ptr
对象解引用的对象,通常与其拥有的指针相同。
The stored pointer
(i.e., the pointer returned by this function) may not be the owned pointer
(i.e., the pointer deleted on object destruction) if the shared_ptr
object is an alias (i.e., alias-constructed
objects and their copies).
如果 shared_ptr
对象是别名 (i.e., alias-constructed
objects and their copies),则 stored pointer
(i.e., the pointer returned by this function) 可能不是 owned pointer
(i.e., the pointer deleted on object destruction)。
A shared_ptr
may share ownership of an object while storing a pointer to another object. get()
returns the stored pointer
, not the managed pointer
.
shared_ptr
可以在存储指向另一个对象的指针时共享对象的所有权。get()
返回存储的指针,而非被管理指针。
none
The stored pointer
.
element_type
is a member type, alias of shared_ptr
's template parameter (T
).
std::shared_ptr::get
//============================================================================
// Name : std::shared_ptr::get
// Author : Yongqiang Cheng
// Version : Version 1.0.0
// Copyright : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================
#include
#include
int main()
{
int* p = new int(10);
std::shared_ptr strong(p);
if (strong.get() == p)
std::cout << "strong and p point to the same location\n";
// four ways of accessing the same address:
std::cout << *strong.get() << "\n";
std::cout << *(strong.get()) << "\n";
std::cout << *strong << "\n";
std::cout << *p << "\n";
return 0;
}
strong and p point to the same location
10
10
10
10
请按任意键继续. . .
assignment [ə'saɪnmənt]:n. 任务,布置,赋值
std::shared_ptr::get
//============================================================================
// Name : std::shared_ptr::get
// Author : Yongqiang Cheng
// Version : Version 1.0.0
// Copyright : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================
#include
#include
void output(std::string msg, int const* pint)
{
std::cout << msg << *pint << "\n";
}
int main()
{
int* pint = new int(10);
std::shared_ptr pshared = std::make_shared(9);
output("Naked pointer: ", pint);
// output("Shared pointer ", pshared); // 编译错误
output("Shared pointer with get(): ", pshared.get());
delete pint;
return 0;
}
1>------ Build started: Project: yongqiangcheng, Configuration: Debug Win32 ------
1> yongqiang.cpp
1>d:\visual_studio_workspace\yongqiangcheng\yongqiangcheng\yongqiang.cpp(14): error C2679: binary '<<': no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
1> d:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(495): note: could be 'std::basic_ostream> &std::basic_ostream>::operator <<(std::basic_streambuf> *)'
1> d:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(475): note: or 'std::basic_ostream> &std::basic_ostream>::operator <<(const void *)'
1> d:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(455): note: or 'std::basic_ostream> &std::basic_ostream>::operator <<(long double)'
1> d:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(435): note: or 'std::basic_ostream> &std::basic_ostream>::operator <<(double)'
1> d:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(415): note: or 'std::basic_ostream> &std::basic_ostream>::operator <<(float)'
1> d:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(395): note: or 'std::basic_ostream> &std::basic_ostream>::operator <<(unsigned __int64)'
1> d:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(375): note: or 'std::basic_ostream> &std::basic_ostream>::operator <<(__int64)'
1> d:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(355): note: or 'std::basic_ostream> &std::basic_ostream>::operator <<(unsigned long)'
1> d:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(335): note: or 'std::basic_ostream> &std::basic_ostream>::operator <<(long)'
1> d:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(315): note: or 'std::basic_ostream> &std::basic_ostream>::operator <<(unsigned int)'
1> d:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(290): note: or 'std::basic_ostream> &std::basic_ostream>::operator <<(int)'
1> d:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(270): note: or 'std::basic_ostream> &std::basic_ostream>::operator <<(unsigned short)'
1> d:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(236): note: or 'std::basic_ostream> &std::basic_ostream>::operator <<(short)'
1> d:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(216): note: or 'std::basic_ostream> &std::basic_ostream>::operator <<(bool)'
1> d:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(209): note: or 'std::basic_ostream> &std::basic_ostream>::operator <<(std::ios_base &(__cdecl *)(std::ios_base &))'
1> d:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(202): note: or 'std::basic_ostream> &std::basic_ostream>::operator <<(std::basic_ios> &(__cdecl *)(std::basic_ios> &))'
1> d:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(196): note: or 'std::basic_ostream> &std::basic_ostream>::operator <<(std::basic_ostream> &(__cdecl *)(std::basic_ostream> &))'
1> d:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(692): note: or 'std::basic_ostream> &std::operator <<>(std::basic_ostream> &,const char *)'
1> d:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(739): note: or 'std::basic_ostream> &std::operator <<>(std::basic_ostream> &,char)'
1> d:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(777): note: or 'std::basic_ostream> &std::operator <<>(std::basic_ostream> &,const char *)'
1> d:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(824): note: or 'std::basic_ostream> &std::operator <<>(std::basic_ostream> &,char)'
1> d:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(950): note: or 'std::basic_ostream> &std::operator <<>(std::basic_ostream> &,const signed char *)'
1> d:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(957): note: or 'std::basic_ostream> &std::operator <<>(std::basic_ostream> &,signed char)'
1> d:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(964): note: or 'std::basic_ostream> &std::operator <<>(std::basic_ostream> &,const unsigned char *)'
1> d:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(971): note: or 'std::basic_ostream> &std::operator <<>(std::basic_ostream> &,unsigned char)'
1> d:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(981): note: or 'std::basic_ostream> &std::operator <<,std::string>(std::basic_ostream> &&,const _Ty &)'
1> with
1> [
1> _Ty=std::string
1> ]
1> d:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(1019): note: or 'std::basic_ostream> &std::operator <<>(std::basic_ostream> &,const std::error_code &)'
1> d:\visual_studio_workspace\yongqiangcheng\yongqiangcheng\yongqiang.cpp(14): note: while trying to match the argument list '(std::ostream, std::string)'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
添加头文件 #include
。
//============================================================================
// Name : std::shared_ptr::get
// Author : Yongqiang Cheng
// Version : Version 1.0.0
// Copyright : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================
#include
#include
#include
void output(std::string msg, int const* pint)
{
std::cout << msg << *pint << "\n";
}
int main()
{
int* pint = new int(10);
std::shared_ptr pshared = std::make_shared(9);
output("Naked pointer: ", pint);
// output("Shared pointer ", pshared); // 编译错误
output("Shared pointer with get(): ", pshared.get());
delete pint;
return 0;
}
Naked pointer: 10
Shared pointer with get(): 9
请按任意键继续. . .
http://www.cplusplus.com/reference/memory/shared_ptr/get/
https://en.cppreference.com/w/cpp/memory/shared_ptr/get