为什么c ++指针*与声明的变量关联,而不是类型? [英] Why does c++ pointer * associate to the variable declared, not the type?

查看:179
本文介绍了为什么c ++指针*与声明的变量关联,而不是类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么C ++设计为在同一行上声明两个int *的正确方法是

  int * x ,* y; 

  int * x,y; 

我知道一些人认为你应该避免任何形式和声明每个变量在自己的行,

解决方案

为了保持与C代码的兼容性,因为这是C的工作原理。 / p>

Bjarne是一个好点这里


int * p; int * p; 不是关于权利和
错,而是关于风格和重点。 C强调表达;
声明通常被认为只是一种必要的邪恶。
另一方面,C ++非常强调类型。



典型的C程序员写 int * p; code>并解释它 * p是
什么是int
强调语法,可以指向C(和C ++)
声明语法来争论风格的正确性。确实,
*绑定到语法中的名称p。



A 典型的C ++程序员 int * p; 并解释它 p是
a指向int
强调类型的指针。实际上,p的类型是int *。
我显然更喜欢重点,并且认为它对于使用C ++的更高级部分的
很重要。


因此,在C ++中这样工作的动机是如何在C中工作。



它在C中工作的动机是,如上所述,C强调表达式而不是类型。


Why was C++ designed such that the correct way to declare two int *s on the same line is

int *x, *y;

not

int* x,y;

I know some people think you should avoid either form and declare every variable on its own line, but I'm interested in why this language decision was made.

解决方案

To keep compatibility with C code, because that's how C works.

Bjarne makes a good point here:

The choice between int* p; and int *p; is not about right and wrong, but about style and emphasis. C emphasized expressions; declarations were often considered little more than a necessary evil. C++, on the other hand, has a heavy emphasis on types.

A typical C programmer writes int *p; and explains it *p is what is the int emphasizing syntax, and may point to the C (and C++) declaration grammar to argue for the correctness of the style. Indeed, the * binds to the name p in the grammar.

A typical C++ programmer writes int* p; and explains it p is a pointer to an int emphasizing type. Indeed the type of p is int*. I clearly prefer that emphasis and see it as important for using the more advanced parts of C++ well.

So, the motivation for this working as this in C++ is how it works in C.

The motivation it works like that in C is that, as stated above, C emphasizes expressions rather than types.

这篇关于为什么c ++指针*与声明的变量关联,而不是类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆