为什么会想到&#39T *名称'是C方式和' T *名称'是C ++方式吗? [英] Why is it thought of 'T *name' to be the C way and 'T* name' to be the C++ way?

查看:79
本文介绍了为什么会想到&#39T *名称'是C方式和' T *名称'是C ++方式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:此问题与星号( * )的位置有关.

Note: This question is about the position of the asterisk (*).

在我看到的大多数C代码中(例如,在 Beej的网络编程指南中),所有变量声明/定义使用 T * name 格式,即,将 * 绑定到变量名称.指针被认为属于变量,而不是类型.

In most C code I see (e.g., in Beej's guide to network programming), all variable declarations / definitions use the T *name format, i.e., bind the * to the variable name. The pointer is thought of belonging to the variable, not the type.

在我看到的大多数C ++代码中,格式为 T *名称,即,它将 * 绑定到变量的类型.该指针被认为属于类型,而不是变量.作为纯C ++编码器,我本人也使用这种格式,因为 pointer-to-type 显然(对我而言)属于类型,而不是变量.(顺便说一下,即使C ++标准在示例中也使用这种格式.:))

In most C++ code I see, the format is T* name, i.e., it binds the * to the type of the variable . The pointer is thought of belonging to the type, not the variable. I myself, as a pure C++ coder, also use this format, as a pointer-to-type clearly (for me) belongs to the type, not the variable. (Incidently, even the C++ standard uses this format in the examples. :) )

这是否有(历史)原因?当程序员开始使用C ++时,思维方式是否已经改变?

Is there a (historic) reason for this? Has the way of thinking just changed when programmers started doing C++?

如果一个C编码器(使用前一种格式)可以解释他/她为何使用它,而不是后者的原因,那也很好.

It would also be nice if a C coder (that uses the former format) could explain on why s/he uses it, and not the latter.

推荐答案

来自 Stroustrup的C ++样式和技术常见问题解答.

典型的C程序员"编写 int * p; 并解释为" * p int ",强调语法,并可能指向C(和C ++)声明语法来证明样式的正确性.实际上, * 绑定到语法中的名称 p .

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.

典型的C ++程序员"编写 int * p; 并解释为" p 是指向 int 的指针",强调类型.实际上, p 的类型是 int * .我显然更喜欢这种强调,并认为它对于很好地使用C ++的更高级部分非常重要.

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.

这篇关于为什么会想到&#39T *名称'是C方式和' T *名称'是C ++方式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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