常量的区别。指针和引用?

时间:2022-11-14 13:29:52

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

干杯

5 个解决方案

#1


44  

There are 3 types of const pointers:

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:

参考文献与以上三种类型的const指针有关键的区别:

  • Const pointers can be NULL.

    Const指针可以为空。

  • A reference does not have it's own address whereas a pointer does.
    The address of a reference is the actual object's address.

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

  • A pointer has its own address and it holds as its value the address of the value it points to.

    指针有它自己的地址,它将它指向的值的地址作为它的值。

  • See my answer here for much more differences between references and pointers.

    有关引用和指针之间的更多差异,请参见这里的答案。

#2


6  

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

我假设您指的是一个const值指针(例如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.
  • 指针也可以指向一个数组,或者它可以是NULL,其中引用总是指向一个对象。
  • The syntax is very different
  • 语法非常不同

#3


6  

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.

引用:默认使用这些。撤销空指针是很常见的。你可以通过引用来消除这种风险。

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 );
}

#4


2  

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

#5


0  

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 can be bound again by casting away the constness.

可以通过丢弃const指针来再次绑定指针。

#1


44  

There are 3 types of const pointers:

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:

参考文献与以上三种类型的const指针有关键的区别:

  • Const pointers can be NULL.

    Const指针可以为空。

  • A reference does not have it's own address whereas a pointer does.
    The address of a reference is the actual object's address.

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

  • A pointer has its own address and it holds as its value the address of the value it points to.

    指针有它自己的地址,它将它指向的值的地址作为它的值。

  • See my answer here for much more differences between references and pointers.

    有关引用和指针之间的更多差异,请参见这里的答案。

#2


6  

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

我假设您指的是一个const值指针(例如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.
  • 指针也可以指向一个数组,或者它可以是NULL,其中引用总是指向一个对象。
  • The syntax is very different
  • 语法非常不同

#3


6  

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.

引用:默认使用这些。撤销空指针是很常见的。你可以通过引用来消除这种风险。

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 );
}

#4


2  

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

#5


0  

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 can be bound again by casting away the constness.

可以通过丢弃const指针来再次绑定指针。