【问题标题】:What are the different types of pointers and references in C++ ? Why are they useful?C++中有哪些不同类型的指针和引用?为什么它们有用?
【发布时间】:2016-06-16 03:01:39
【问题描述】:

尽可能简单地解释?

【问题讨论】:

标签: c++ pointers c++11 reference smart-pointers


【解决方案1】:

在本教程中让 T 成为一个类 C++中的指针可以分为3种类型:

1) 原始指针

T a;  
T * _ptr = &a; 

它们将内存地址保存到内存中的某个位置。谨慎使用,因为程序变得复杂难以跟踪。

带有 const 数据或地址的指针 { 向后读取 }

T a ; 
const T * ptr1 = &a ; 
T const * ptr1 = &a ;

指向数据类型 T 的指针,它是一个 const。这意味着您不能使用指针更改数据类型。即*ptr1 = 19;不管用。但是你可以移动指针。即ptr1++ , ptr1--;等会工作。 向后阅读:指向类型 T 的指针,即 const

  T * const ptr2 ;

指向数据类型 T 的 const 指针。这意味着您不能移动指针,但可以更改指针指向的值。即*ptr2 = 19 将起作用,但ptr2++ ; ptr2-- 等将不起作用。向后阅读:指向类型 T 的 const 指针

const T * const ptr3 ; 

指向 const 数据类型 T 的 const 指针。这意味着您不能移动指针,也不能将数据类型指针更改为指针。 IE 。 ptr3-- ; ptr3++ ; *ptr3 = 19; 不起作用

3) 智能指针 : { #include <memory> }

共享指针

  T a ; 
     //shared_ptr<T> shptr(new T) ; not recommended but works 
     shared_ptr<T> shptr = make_shared<T>(); // faster + exception safe

     std::cout << shptr.use_count() ; // 1 //  gives the number of " 
things " pointing to it. 
     T * temp = shptr.get(); // gives a pointer to object

     // shared_pointer used like a regular pointer to call member functions
      shptr->memFn();
     (*shptr).memFn(); 

    //
     shptr.reset() ; // frees the object pointed to be the ptr 
     shptr = nullptr ; // frees the object 
     shptr = make_shared<T>() ; // frees the original object and points to new object

实现使用引用计数来跟踪有多少“事物”指向指针所指向的对象。当此计数变为 0 时,该对象被自动删除,即当所有指向该对象的 share_ptr 超出范围时,objected 被删除。 这消除了必须删除使用 new 分配的对象的麻烦。

弱指针: 帮助处理使用共享指针时出现的循环引用 如果您有两个由两个共享指针指向的对象,并且有一个内部共享指针指向彼此的共享指针,那么将有一个循环引用,并且当共享指针超出范围时,该对象不会被删除。要解决此问题,请将内部成员从 shared_ptr 更改为 weak_ptr。注意:要访问弱指针指向的元素,请使用 lock() ,这将返回一个weak_ptr。

T a ; 
shared_ptr<T> shr = make_shared<T>() ; 
weak_ptr<T> wk = shr ; // initialize a weak_ptr from a shared_ptr 
wk.lock()->memFn() ; // use lock to get a shared_ptr 
//   ^^^ Can lead to exception if the shared ptr has gone out of scope
if(!wk.expired()) wk.lock()->memFn() ;
// Check if shared ptr has gone out of scope before access

见:When is std::weak_ptr useful?

唯一指针: 具有专有所有权的轻量级智能指针。当指针指向唯一对象而不在指针之间共享对象时使用。

unique_ptr<T> uptr(new T);
uptr->memFn(); 

//T * ptr = uptr.release(); // uptr becomes null and object is pointed to by ptr
uptr.reset() ; // deletes the object pointed to by uptr 

要更改唯一 ptr 指向的对象,请使用移动语义

unique_ptr<T> uptr1(new T);
unique_ptr<T> uptr2(new T);
uptr2 = std::move(uptr1); 
// object pointed by uptr2 is deleted and 
// object pointed by uptr1 is pointed to by uptr2
// uptr1 becomes null 

参考资料: 它们本质上可以看作 const 指针,即一个 const 指针,不能用更好的语法移动。

见:What are the differences between a pointer variable and a reference variable in C++?

r-value reference : reference to a temporary object   
l-value reference : reference to an object whose address can be obtained
const reference : reference to a data type which is const and cannot be modified 

参考: https://www.youtube.com/channel/UCEOGtxYTB6vo6MQ-WQ9W_nQ

【讨论】:

  • 虽然我喜欢你的概述,为什么不把它贴在already existing question 上呢?在那里你会得到我的支持;)
猜你喜欢
  • 2012-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-30
  • 1970-01-01
  • 2022-07-22
相关资源
最近更新 更多