类型声明 - 指针星号位置 [英] Type Declaration - Pointer Asterisk Position

查看:312
本文介绍了类型声明 - 指针星号位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,下面的意思是为int指针分配内存:

in C++, the following means "allocate memory for an int pointer":

int* number;

所以,星号是变量类型的一部分;没有它,这将意味着别的东西(这就是为什么我通常不分离星号和变量类型)。那么什么原因星号被认为是别的东西,而不是类型的一部分?例如,看起来更好,如果下面的意思是为两个int指针分配内存:

So, the asterisk is part of the variable type; without it, that would mean something else (that's why I usually don't separate the asterisk from the variable type). Then what is the reason the asterisk is considered something else, instead of being part of the type? For example, it seems better, if the following meant "allocate memory for two int pointers":

int* number1, number2;

我错了吗?

推荐答案

Stroustrup被问及他说(释义)

Stroustrup was asked this and he said (paraphrasing)



  • 更多的C-ish,你会说 int * a Employee * pE
    (所以在你的头脑中,你在想 / strong>是一个整数)

  • 如果您认为更多C ++ - ish,您会说 int * a Employee * pE
    (所以在你的头部是 a 是一个整数指针)

  • if you think more C-ish you will say int *a and Employee *pE (so in your head you're thinking "the content of a is an integer")
  • if you think more C++-ish you will say int* a and Employee* pE (so in your head it's "a is an integer pointer")

只要你永远不会在同一行上声明两个
ponters,你就会这么想。

You can think however you like, as long as you never declare two ponters on the same line.

适用于我。我是一个员工* pE 的人,但我结婚了员工* pE - 我的建议不会太忙了。

Works for me. I'm an Employee* pE kind of person, but I'm married to an Employee *pE kind of person - my advice would be not to get too worked up about it.

这篇关于类型声明 - 指针星号位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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