C++ 关于「;int const*p";及;常数int*p";
在我看来,这意味着p2不能修改,而p2点可以更改,对吗? 为什么 p2=&i2 可以编译吗 关于这一行: 常量int常量*p3=&i1 p3=&i2 哦,上帝。。。我疯了。我不知道为什么这一行可以编译没有错误。。。 有人能帮我吗 另一个让我困惑的代码是:C++ 关于「;int const*p";及;常数int*p";,c++,constants,C++,Constants,在我看来,这意味着p2不能修改,而p2点可以更改,对吗? 为什么 p2=&i2 可以编译吗 关于这一行: 常量int常量*p3=&i1 p3=&i2 哦,上帝。。。我疯了。我不知道为什么这一行可以编译没有错误。。。 有人能帮我吗 另一个让我困惑的代码是: int const *p2 = &i1; 为什么可以编译此代码? 在 int常量*getP()常量 我已经更改了值或*p 否,*前面的const关键字表示您指向的变量是“const”变量,只有它不能修改 如果您想要一个无法重新分配的指
int const *p2 = &i1;
为什么可以编译此代码?
在
int常量*getP()常量
我已经更改了值或*p 否,*前面的
const
关键字表示您指向的变量是“const”变量,只有它不能修改
Foo*const p=&bar代码>
const Foo*const p=&bar
将
const int*foo
的指针分配给const int*const bar
的指针是非常好的,就像将int
的值分配给const int
一样。用同样的方式来考虑它。int const*与const int*相同。int const*p
和const int*p
是相同的。当常量
位于*
之后时
表达式的语义发生了变化
我知道,这太疯狂了
class Coo2
{
public:
Coo2() : p(new int(0)) {}
~Coo2() {delete p;}
int const * getP() const
{
*p = 1;
return this->p;
}
private:
int* p;
};
它们都声明指向常量数据的非常量指针
也就是说,使用p
,您无法更改它所指向的数据。但是,您可以更改指针本身,例如,通过指定为合法的p=&i2
。但是*p=87987
是非法的,因为数据p
指向的是常量
--
这将声明指向非常量数据的常量指针。也就是说,p=&i2
是非法的,但是*p=98789
是合法的
--
这将声明指向常量数据的常量指针。也就是说,现在
p=&i2
和*p=87897
都是非法的。重要的是限定符相对于星号的位置(*
):
在指针的帮助下,您实际上可以做两件事
int const *p; // normal pointer to const int
const int *p; // ditto
int *const p; // const pointer to normal int (rarely useful)
int const * const p; // const pointer to const int
对于,实际上无法更改到其他位置,即指向常量位置但能够修改数据的指针,语义应为int*const
。因为指针的内容是一个常量,所以应该在声明时初始化它
const int num = 5; // Both mean the same.
int const num = 5;
然而,还有第三种。指向常量位置的常量指针,该指针既不能指向其他内存位置,也不能更改它所指向的数据。(即常量int*const)
现在回答这些问题,可以对前两个进行编译,因为它们没有指向固定的位置。因此,它们也可以在以后的阶段进行修改
int num = 5;
int* const ptr; // Wrong
ptr = # // Wrong
int* const ptr = #
*ptr = 100;
在上面的代码段中,p3
是指向常量位置的常量指针。因此,它不能被修改
成员函数末尾的const
表示它不会更改对象的状态。当你说*p=1代码>,您没有更改对象的状态p
仍然指向相同的内存位置。这是不允许的-
const int const *p3 = &i1;
p3 = &i2; // Wrong
希望,现在你了解程序编译的原因:) 这里,我们考虑4种指针声明:
int*w代码>
这意味着w是指向整型值的指针。我们可以修改指针及其内容。如果我们初始化w,而声明如下:
int*w=&a代码>
然后,以下两种操作都是可行的:
w=&b代码>(正确)
*w=1代码>(正确)
int*const x代码>
这意味着x是一个指向整型值的常量指针。如果我们初始化x,而声明如下:
int*const x=&a代码>
然后,我们不能这样做:x=&b;(错误)
,因为x是一个常量指针,无法修改。
但是,也可以这样做:*x=1;(true)
,因为x的内容不是常数
*p = 3;
int const*y代码>//两者的意思相同
const int*y代码>
这意味着y是指向常量整数值的指针。如果我们初始化y,而声明如下:
int const*y=&a代码>
然后,可以执行以下操作:y=&b;(true)
因为y是一个可以指向任何地方的非常量指针。
但是,我们不能这样做:*y=1;(错误)
,因为y指向的变量是常量变量,无法修改
int const*const z代码>//两者的意思相同
const int*const z代码>
这意味着z是一个指向常量整数值的常量指针。如果我们在如下声明中初始化z:
int const*const z=&a代码>
因此,以下任何一种操作都是可行的:
z=&b;(错误)
*z=1;(错误)
简洁;读/写int和指针的每个组合
int const * Coo2::getP() const
{
*p = 1; // State of `p` is still not modified.
p = new int ; // Error: Changing the memory location to which p points.
// This is what changing the state of object mean and
// is not allowed because of `const` keyword at the end of function
return this->p;
}
以下是指向常量的指针:
int main() {
int a,b;
int* w; // read/write int, read/write pointer
w= &b; // good
*w= 1; // good
int* const x = &a; // read only pointer, read/write int
// x = &b; // compilation error
*x = 0; // good
int const * y; // read/write ptr, read only int
const int * y2; // " " "
y = &a; // good
// *y = 0; // compilation error
y2 = &a; // good
// *y2 = 0; // compilation error
int const * const z = &a; // read only ptr and read only int
const int * const z2 = &b; // " " " "
// *z = 0; // compilation error
// z = &a; // compilation error
// *z2 = 0; // compilation error
// z2 = &a; // compilation error
}
const int* p;
*p = 3;
以下语句是非法的,因为它试图更改
常数的值:
int main() {
int a,b;
int* w; // read/write int, read/write pointer
w= &b; // good
*w= 1; // good
int* const x = &a; // read only pointer, read/write int
// x = &b; // compilation error
*x = 0; // good
int const * y; // read/write ptr, read only int
const int * y2; // " " "
y = &a; // good
// *y = 0; // compilation error
y2 = &a; // good
// *y2 = 0; // compilation error
int const * const z = &a; // read only ptr and read only int
const int * const z2 = &b; // " " " "
// *z = 0; // compilation error
// z = &a; // compilation error
// *z2 = 0; // compilation error
// z2 = &a; // compilation error
}
const int* p;
*p = 3;
但这是合法的,因为指针本身不是常数:
int main() {
int a,b;
int* w; // read/write int, read/write pointer
w= &b; // good
*w= 1; // good
int* const x = &a; // read only pointer, read/write int
// x = &b; // compilation error
*x = 0; // good
int const * y; // read/write ptr, read only int
const int * y2; // " " "
y = &a; // good
// *y = 0; // compilation error
y2 = &a; // good
// *y2 = 0; // compilation error
int const * const z = &a; // read only ptr and read only int
const int * const z2 = &b; // " " " "
// *z = 0; // compilation error
// z = &a; // compilation error
// *z2 = 0; // compilation error
// z2 = &a; // compilation error
}
const int* p;
*p = 3;
另一方面,此声明显示指向变量的常量指针
p = &x;
在这种情况下,以下语句是合法的,因为变量可以更改:
int* const p;
但这个不是,因为它试图改变常数的值<