自动关键字和顶级、低级常量 在C++引物中,有提到 自动通常忽略顶级常量。通常,在初始化过程中,会保留低级常量,例如当初始化器是指向常量的指针时 const int ci = i,&cr=ci; auto b=ci; //ok:b is an int (top level constants in ci is ignored) auto d=&i; //d is an int* (& of a int object is int *) auto e=&ci; // e is a const int *(& of a constant object is a low level constant)

自动关键字和顶级、低级常量 在C++引物中,有提到 自动通常忽略顶级常量。通常,在初始化过程中,会保留低级常量,例如当初始化器是指向常量的指针时 const int ci = i,&cr=ci; auto b=ci; //ok:b is an int (top level constants in ci is ignored) auto d=&i; //d is an int* (& of a int object is int *) auto e=&ci; // e is a const int *(& of a constant object is a low level constant),c++,constants,auto,C++,Constants,Auto,现在,我的问题是:在第二个语句中,const被忽略,b的类型是int。 但在最后一条语句中,不会忽略ci的常量,并且类型是const int*,而不是int*。为什么??当您使用auto b=ci;,你创建了一个CI的拷贝。所以C++没有理由阻止你改变B的值。 但如果使用auto e=&ci;,您将创建常量int变量ci的指针。e应该是常量上的指针,以防止您更改ci的值。当您使用auto b=ci;,你创建了一个CI的拷贝。所以C++没有理由阻止你改变B的值。 但如果使用auto e=&ci;,

现在,我的问题是:在第二个语句中,const被忽略,b的类型是int。 但在最后一条语句中,不会忽略ci的常量,并且类型是const int*,而不是int*。为什么??

当您使用auto b=ci;,你创建了一个CI的拷贝。所以C++没有理由阻止你改变B的值。 但如果使用auto e=&ci;,您将创建常量int变量ci的指针。e应该是常量上的指针,以防止您更改ci的值。

当您使用auto b=ci;,你创建了一个CI的拷贝。所以C++没有理由阻止你改变B的值。
但如果使用auto e=&ci;,您将创建常量int变量ci的指针。e应该是常量值上的指针,以防止更改ci的值。

它是低级常量,因为它直接引用它。auto b=ci将ci的值复制到b,但auto e=&ci必须具有一些常量,因为它不复制ci,而是指向ci存在的位置。在这种情况下,术语low level意味着没有太多的间接性。

它是一个low level常量,因为它直接引用它。auto b=ci将ci的值复制到b,但auto e=&ci必须具有一些常量,因为它不复制ci,而是指向ci存在的位置。在这种情况下,术语low level意味着没有太多的间接性

const int i = ...;
长期以来,定义常量对象一直是一个令人困惑的问题。让人觉得,

const int* ptr = ...; 
还定义了一个常量指针。这将是一个错误的结论。如果你稍微移动一下常量,就不会那么混乱了

int i = ...;                 // Defines i to be a non-const object
int const i = ...;           // Defines i to be a const object

int* ptr = ...;              // Defines ptr to be a non-const pointer to a non-const object
int const* ptr = ...;        // Defines ptr to be a non-const pointer to a const object
int* const ptr = ...;        // Defines ptr to be a const pointer to a non-const object
int const* const ptr = ...;  // Defines ptr to be a const pointer to a const object
谈到顶级简历资格赛

int const i = ...;
定义类型为int且具有常量限定符的对象

int volatile i = ...;
int const* ptr = ...;
int * const ptr = ...;
定义一个类型为int且具有volatile限定符的对象

int volatile i = ...;
int const* ptr = ...;
int * const ptr = ...;
定义一个类型为int const*但没有const或volatile限定符的对象。第二级类型int具有常量限定符,但不是顶级类型

int const* const ptr = ...;
定义一个类型为int const*且具有const限定符的对象。第二级类型int也有const限定符

int volatile i = ...;
int const* ptr = ...;
int * const ptr = ...;
定义类型为int*且具有常量限定符的对象。第二级类型int没有常量限定符

int volatile i = ...;
int const* ptr = ...;
int * const ptr = ...;
更多信息:

使用

const int i = ...;
长期以来,定义常量对象一直是一个令人困惑的问题。让人觉得,

const int* ptr = ...; 
还定义了一个常量指针。这将是一个错误的结论。如果你稍微移动一下常量,就不会那么混乱了

int i = ...;                 // Defines i to be a non-const object
int const i = ...;           // Defines i to be a const object

int* ptr = ...;              // Defines ptr to be a non-const pointer to a non-const object
int const* ptr = ...;        // Defines ptr to be a non-const pointer to a const object
int* const ptr = ...;        // Defines ptr to be a const pointer to a non-const object
int const* const ptr = ...;  // Defines ptr to be a const pointer to a const object
谈到顶级简历资格赛

int const i = ...;
定义类型为int且具有常量限定符的对象

int volatile i = ...;
int const* ptr = ...;
int * const ptr = ...;
定义一个类型为int且具有volatile限定符的对象

int volatile i = ...;
int const* ptr = ...;
int * const ptr = ...;
定义一个类型为int const*但没有const或volatile限定符的对象。第二级类型int具有常量限定符,但不是顶级类型

int const* const ptr = ...;
定义一个类型为int const*且具有const限定符的对象。第二级类型int也有const限定符

int volatile i = ...;
int const* ptr = ...;
int * const ptr = ...;
定义类型为int*且具有常量限定符的对象。第二级类型int没有常量限定符

int volatile i = ...;
int const* ptr = ...;
int * const ptr = ...;
更多信息:

你忘了提到int i=0; 在这里,我可以在程序的上下文中进行更改,使其成为非常量

在第二条语句中,const被忽略

因此,没有可以忽略的顶级常量

C++中的实际代码,< /P>
int i = 0;//Value of i is 0 but it might change
const int ci = i;//Value of ci will not change once initialized
auto d = &i;//Hence d is an int*(No top-level const)
在最后一条语句中,不忽略ci常量

&const对象的值是一个低级const,auto不会忽略它

auto e = &ci;// e is const int*(low-level const)
你忘了提到int i=0; 在这里,我可以在程序的上下文中进行更改,使其成为非常量

在第二条语句中,const被忽略

因此,没有可以忽略的顶级常量

C++中的实际代码,< /P>
int i = 0;//Value of i is 0 but it might change
const int ci = i;//Value of ci will not change once initialized
auto d = &i;//Hence d is an int*(No top-level const)
在最后一条语句中,不忽略ci常量

&const对象的值是一个低级const,auto不会忽略它

auto e = &ci;// e is const int*(low-level const)

显示iyou的声明回答了您自己的问题,在&ci之后,常量是一级间接距离,但只有顶级常量是忽略的i;我所知道的是,如果对象本身是常数,那么常数是顶级的,否则是低级的。我不知道这些术语到底是什么意思。我没听清你的话,一级间接距离?一级间接距离在所有这一切中到底意味着什么。你回答了你自己的问题,在&ci之后,常数是一级间接距离,但只有顶级常数是忽略的;我所知道的是,如果对象本身是常数,那么常数是顶级的,否则是低级的。我不知道这些术语到底是什么意思。我没听清你的话,一个间接层次?层次意味着
在所有这一切中,间接层次到底意味着什么。e不是常数,e的类型是常数int*。它是constant@NamanSharma常量int*不是常量。它指向一个常量int。我知道这件事。告诉我一件事:&一个常量对象的值是低级常量。你想知道为什么e的类型是常量int*,而不是int*,对吗?e不是常量e的类型是常量int*。它是constant@NamanSharma常量int*不是常量。它指向一个常量int。我知道这件事。告诉我一件事:&一个常量对象的值是低级常量。你想知道为什么e的类型是常量int*,而不是int*,对吗?