C++ 常量之间的区别。指针和引用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2336230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:06:14  来源:igfitidea点击:

Difference between const. pointer and reference?

c++referencepointers

提问by Arnkrishn

What is the difference between a constant pointer and a reference?

常量指针和引用有什么区别?

Constant pointer as the name implies can not be bound again. Same is the case with the reference.

常量指针顾名思义是不能再绑定的。引用的情况也是如此。

I wonder in what sort of scenarios would one be preferred over the other. How different is their C++ standard and their implementations?

我想知道在哪种情况下,一种会比另一种更受欢迎。他们的 C++ 标准和实现有何不同?

cheers

干杯

回答by Brian R. Bondy

There are 3 types of const pointers:

有 3 种类型的 const 指针:

//Data that p points to cannot be changed from p
const char* p = szBuffer;

//p cannot point to something different.  
char* const p = szBuffer;

//Both of the above restrictions apply on p
const char* const p = szBuffer;

Method #2 above is most similar to a reference.

上面的方法#2 与参考文献最相似。

There are key differences between references and all of the 3 types of const pointers above:

引用和上述所有 3 种类型的 const 指针之间存在主要区别:

  • 常量指针可以为 NULL。

  • 引用没有自己的地址,而指针有。
    引用的地址是实际对象的地址。

  • 一个指针有它自己的地址,它把它指向的值的地址作为它的值。

  • 有关引用和指针之间的更多差异,请参阅我的回答

回答by Tronic

I assume that you mean a const-valued pointer (e.g. int* const ptr), not a pointer to const (e.g. int const* ptr).

我假设您的意思是常量值指针(例如 int* const ptr),而不是指向 const 的指针(例如 int const* ptr)。

  • Not initializing a reference is a compile error (avoids the problem of uninitialized pointers)
  • A pointer may also point to an array, or it can be NULL, where a reference always refers to exactly one object.
  • The syntax is very different
  • 不初始化引用是编译错误(避免了未初始化指针的问题)
  • 一个指针也可以指向一个数组,或者它可以是 NULL,其中一个引用总是指向一个对象。
  • 语法非常不同

回答by thebretness

When you should use each:

当你应该使用每个:

reference: Use these by default. It is very common for people to dereference NULL pointers. You eliminate that risk with a reference.

参考:默认使用这些。人们取消引用 NULL 指针是很常见的。您可以通过参考消除这种风险。

const pointer: When you want a reference, but can't make one. For example, you are writing a driver, and you'd like a pointer to the beginning of a memory map. A reference doesn't make as much sense in that case. Also, if you need an array of the things, a reference won't work (though an array of simple classes with reference members will).

const 指针:当您想要一个引用,但无法创建时。例如,您正在编写一个驱动程序,并且您想要一个指向内存映射开头的指针。在这种情况下,引用没有多大意义。此外,如果您需要一组事物,则引用将不起作用(尽管具有引用成员的简单类数组将起作用)。

In the next example, a const pointer checks an error that a reference can't check:

在下一个示例中,const 指针检查引用无法检查的错误:

int addFour( int* register ){
  if(isNull(arg)){
    throw NullPointerException();
  }  

  // some stuff
  *register += 4;

  return register;
}

// This could be any function that does pointer math.
bool isNull(const int* ptr){
  return( NULL == ptr );
}

回答by Gaurav Singh

Almost all points have been covered by other answers, except this important one : It is possible to do arithmetics on pointers, but not on reference. E.g.

除了这个重要的答案之外,几乎所有的观点都被其他答案所涵盖:可以对指针进行算术运算,但不能对引用进行算术运算。例如

int a[3] = {39, 18, 97};
int * const b = a;
int c = *(b+1);  // sets c = 18

回答by Andrew Stein

Some differences:

一些差异:

A const pointer can point to NULL.

const 指针可以指向 NULL。

A const point can point to an array of objects.

const 点可以指向一个对象数组。

A const pointer canbe bound again by casting away the constness.

可以通过抛弃常量性来再次绑定常量指针。