尽管获取了错误数据类型的地址,代码仍能编译

发布于 2024-10-22 00:38:08 字数 331 浏览 1 评论 0原文

#include <iostream>
#define n 255

using namespace std;


int main()
{
int i=n;
int *ptr=&i;
int const *ptr_1=&i;
const int *ptr_2=&i;
const int * const ptr_3=&i;
}

为什么此代码可以在 Visual C++、Dev C++ 和 G++ 中编译? 链接到 - Ideone -

#include <iostream>
#define n 255

using namespace std;


int main()
{
int i=n;
int *ptr=&i;
int const *ptr_1=&i;
const int *ptr_2=&i;
const int * const ptr_3=&i;
}

Why does this Code Compile in Visual C++, Dev C++ and G++?
Link to - Ideone -

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

故事与诗 2024-10-29 00:38:08

它确实可以编译,因为所有这些指针都不会更改变量访问的 const 限定或强制限定,但它们都不会尝试放松限定。

例如:

const int var;
int* address = &var; //illegal - tries to remove const

尝试对 const 变量进行非常量访问,这是非法的,但

int var;
const int* address = &var; //legal - only adds const

尝试对非常量变量进行常量访问,这是合法的,因为非常量变量确实允许仅 const 访问。

It does compile, because all those pointers ether don't change const-qualification of the variable access or enforce the qualification, but none of them tries to loosen the qualification.

For example:

const int var;
int* address = &var; //illegal - tries to remove const

would try to get non-const access to a const variable and that's illegal, but

int var;
const int* address = &var; //legal - only adds const

tries to get const access to a non-const variable and that's legal since a non-const variable does allow const-only access.

默嘫て 2024-10-29 00:38:08

因为没有什么问题吗?

两边的类型相同: int *ptr=&i;

更多 const,完全没问题: int const *ptr_1=&i;

与上面的行完全相同: const int *ptr_2=&i;

更多 const,完全没问题: const int * const ptr_3=&i;

你总是可以创建一个变量 more< /strong> 常量。

int const * i;
int * j = i;

上面的内容使 j less const 并且无效。

4.4/1:(涵盖 int *int const *

“指向 cv1 T 的指针”类型的右值
可以转换为类型的右值
如果“cv2 T”大于“指向 cv2 T”的指针
简历比“cv1 T”合格。

Because there's nothing wrong with it?

Same types on both side: int *ptr=&i;

More const, perfectly ok: int const *ptr_1=&i;

Exactly the same as above line: const int *ptr_2=&i;

More const, perfectly ok: const int * const ptr_3=&i;

You can always make a variable more const.

int const * i;
int * j = i;

The above makes j less const and isn't valid.

4.4/1: (Covers int * to int const *)

An rvalue of type “pointer to cv1 T”
can be converted to an rvalue of type
“pointer to cv2 T” if “cv2 T” is more
cv-qualified than “cv1 T.”

痴情 2024-10-29 00:38:08

您可能想知道为什么在获取地址时可以将值设置为 const。

这是合法的,在 C++ 术语中称为 const 提升

请注意,仅一个级别const 提升可能会发生 - 也就是说,int** 可能会提升为 int * const* 但不是 int const**const 必须向左“移动”两个位置)。

You are probably wondering why the value can be made const when taking the address.

This is legal and is known as const promotion in C++ parlance.

Note that only one level of const promotion may happen – that is, a int** may be promoted to int* const* but not to int const** (the const would have to “move” two places to the left).

梦明 2024-10-29 00:38:08

C++ 允许您创建指向在堆栈上创建的变量的指针。因此,这段代码没有任何问题。而且,它经常被用来向WINAPI等函数传递参数。

如果问题是关于指针的语义,那么答案是成熟的。
例如:

int * ptr; //pointer to a variable. Pointer and the variable can be changed.
int const * ptr; //a pointer to the constant variable, a pointer can be changed, the variable can not.
const int * ptr; //same as above.
const int * const ptr_3; //the most interesting section, a const pointer to const variable. 

只要记住 * 之前的修饰符指的是变量,后面的修饰符 - 指的是指针。

C++ allows you to create pointers to the variables created on the stack. Thus, there is nothing wrong with this code. Moreover, it is often used to pass parameters to functions such as WINAPI.

If the question about the semantics of pointers then the answer is a full-fledged.
For example:

int * ptr; //pointer to a variable. Pointer and the variable can be changed.
int const * ptr; //a pointer to the constant variable, a pointer can be changed, the variable can not.
const int * ptr; //same as above.
const int * const ptr_3; //the most interesting section, a const pointer to const variable. 

Just remember that the modifiers before * refer to the variable, after it - to the pointer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文