* 和 & 是什么意思什么时候应用于变量名?
在 C++ 中,以下之间有什么区别:
void func(MyType&); // declaration
//...
MyType * ptr;
func(*ptr); // compiler doesnt give error
func(ptr); // compiler gives error i thought & represents memory address so
// this statement should correct as ptr is only a pointer
// or address of some real var.
In C++, what is the difference between:
void func(MyType&); // declaration
//...
MyType * ptr;
func(*ptr); // compiler doesnt give error
func(ptr); // compiler gives error i thought & represents memory address so
// this statement should correct as ptr is only a pointer
// or address of some real var.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一元前缀运算符
&
应用于对象时,会生成地址 > 对象:&obj
.类型修饰符
&
,当应用于即将声明的变量时,会将变量的类型修改为 >引用类型:int&
。这同样适用于
*
:当作为一元前缀运算符应用于指针时,它将取消引用 指针,产生引用的对象:*ptr
。当用作要声明的变量的类型修饰符时,
*
会将类型修改为指针:int*
。类似地,应用于正在声明的变量的类型修饰符
[]
会将变量的类型修改为数组,而<应用于数组类型的对象的strong>二元中缀运算符[]
将访问该数组的子对象之一。类型修饰符应用于声明的变量,而不是应用于声明它们的类型,这没有什么帮助。例如,这
定义了一个
int
指针、一个指向int
指针的指针、一个普通的int
、一个包含 10 个的数组int
和一个int
引用。 (后者会立即初始化,因为您不能拥有未初始化的引用。)请注意,类型修饰符在语法上属于它们要修改其类型的声明变量,而不是属于声明变量的类型。尽管如此,类型修饰符(*
和&
)会修改变量的类型。但是,在以下情况下,
p
、i
和a
被假定为已声明的变量* 和
&
是一元前缀运算符,解引用pp
并生成i
的地址,而[]
生成数组a
中的第一个int
对象。事实上,C 和 C++ 并不关心类型修饰符周围的空格,这导致了在放置它们时出现不同的阵营并没有真正让事情变得更容易。
有些人将类型修饰符放在靠近类型的位置。他们认为它修改了类型,因此应该放在那里:
缺点是在声明多个对象时这会变得混乱。这
将
a
定义为指向int
的指针,而将b
定义为int
。这就是为什么有些人更喜欢写“我建议永远不要在同一个语句中声明多个对象”。 IMO 使代码更易于阅读。此外,您还可以自由选择任一约定。
让事情变得更复杂的是,除了类型修饰符和一元前缀运算符
&
和*
之外,还有还有二元中缀运算符&
和*
,意思是“按位与”和“乘法”。雪上加霜的是,在 C++ 中,您可以重载 这些运算符的一元前缀和二进制中缀变体(以及二进制中缀[]
)对于用户定义的类型,并且其语义完全自由。The unary prefix operator
&
, when applied to an object, yields the address of the object:&obj
.The type modifier
&
, when applied to a variable about to be declared, will modify the variable's type to be a reference type:int&
.The same applies to
*
: When applied as a unary prefix operator to a pointer, it will dereference the pointer, yielding the object referred to:*ptr
.When used as a type modifier to a variable about to be declared,
*
will modify the type to be a pointer:int*
.In a similar way, the type modifier
[]
applied to a variable that's being declared will modify the variable's type to an array, while the binary infix operator[]
applied to an object of array type will access one of the array's sub-objects.It's not helpful that type modifiers apply to the variable that is declared, not to the type they are declared with. For example, this
defines an
int
pointer, a pointer to a pointer to anint
, a vanillaint
, an array of 10int
, and anint
reference. (The latter is immediately initialized, because you cannot have an uninitialized reference.) Note that the type modifiers syntactically belong to the declared variable whose type they are modifying, not to the declared variable's type. Nevertheless, type modifiers (*
and&
) modify the type of the variable.In the following case, however, with
p
,i
, anda
presumed to be variables that have already been declared*
and&
are unary prefix operators dereferencingpp
and yielding the address ofi
, while[]
yields the firstint
object in the arraya
.The fact that C and C++ don't care about the whitespaces around type modifiers and that this led to different camps when it comes to place them doesn't really make things easier.
Some people place the type modifiers close to the type. They argue that it modifies the type and so it should go there:
The disadvantage is that this becomes confusing when declaring several objects. This
defines
a
to be a pointer toint
, butb
to be anint
. Which is why some people prefer to writeI suggest to simply never declare multiple objects in the same statement. IMO that makes code easier to read. Also, it leaves you free to pick either convention.
To complicate things even further, besides the type modifiers and the unary prefix operators
&
and*
, there are also the binary infix operators&
and*
, meaning "bitwise AND" and "multiplication". And to add insult to injury, in C++ you can overload both the unary prefix and the binary infix variants of these operators (and the binary infix[]
) for user-defined types and be completely free as to their semantics.