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

发布于 2024-10-06 06:43:39 字数 447 浏览 3 评论 0原文

可能的重复:
C++中指针变量和引用变量的区别 < /p>

这个问题的延续

引用变量只是const指针变量的另一个名称?如果它们不同,声明为引用变量的变量和声明为 const 指针变量的变量之间有什么区别?

Possible Duplicate:
Difference between pointer variable and reference variable in C++

This is a continuation of this question

Is a reference variable just another name of const pointer variable? If they are different what is the difference between a variable declared as a reference variable and a variable declared as a const pointer variable?

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

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

发布评论

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

评论(3

缘字诀 2024-10-13 06:43:39

只是为了好玩:) 引用不能为 NULL,但 const 指针可以。

Just for fun:) A reference can not be NULL but a const pointer can be.

你在我安 2024-10-13 06:43:39

好的。问题是没有像引用变量这样的东西。引用根本不是变量。它不是一个物体。它实际上根本没有尺寸。它只是原始对象的替代名称。

检查一下:

struct A
{
   int i[5];
};

int main()
{
   std::cout << (sizeof(A&) == sizeof(A)) << std::endl;
   std::cout << (typeid(A&) == typeid(A)) << std::endl;
   return 0;
}

A& has the same size as A
A& has the same type as A

OK. The problem is that there is no such thing like reference variable. Reference is not a variable at all. It is not an object. It actually has no size at all. It is just alternative name of the original object.

Check this:

struct A
{
   int i[5];
};

int main()
{
   std::cout << (sizeof(A&) == sizeof(A)) << std::endl;
   std::cout << (typeid(A&) == typeid(A)) << std::endl;
   return 0;
}

A& has the same size as A
A& has the same type as A
北方的韩爷 2024-10-13 06:43:39

引用变量只是 const 指针变量的另一个名称吗?

不。

如果不同,声明为引用变量的变量和声明为 const 指针变量的变量有什么区别?

它们是完全不同的东西,这里没有足够的空间来解释您需要了解的所有内容。阅读本文。实际上,请阅读整个常见问题解答。你会学到很多东西。

Is a reference variable just another name of const pointer variable?

No.

If they are different what is the difference between a variable declared as a reference variable and a variable declared as a const pointer variable?

They are completely different things, and there is not enough room to explain everything you need to know here. Read this. Actually, read the entire FAQ. You'll learn a lot.

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