为什么不允许指针的运算符重载工作? [英] Why isn't operator overloading for pointers allowed to work?

查看:30
本文介绍了为什么不允许指针的运算符重载工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据这个答案下的评论,引用主要是为了支持运算符重载,引用 Bjarne Stroustrup:

<块引用>

参考文献主要介绍支持运算符重载.C通过每个函数参数传递值,以及传递对象的地方价值将是低效的或不合适的用户可以通过指针.这个策略行不通其中使用运算符重载.在在这种情况下,符号方便是必不可少,因此用户不能预计插入地址-如果对象很大,则运算符.

这意味着运算符重载不能与指针一起使用.但是它并没有清楚地解释为什么用指针重载的运算符不能工作.为什么运算符重载对指针不起作用?

IMO 在使用引用的地方,也可以在其位置使用指针.

解决方案

因为如果允许,那么它看起来就不好,也不会像参考那样直观.

假设允许,那么你会写:

struct A{};A a, *pa, b;a = pa ;//不好看,也不直观.(不是真正的 C++)

它看起来不太好,因为在左侧你没有指针,在右侧你有指针.看起来非常非常奇怪.此外,由于类型不匹配,因此它的具体功能看起来不是很直观.我的意思是,您将指针分配给非指针;这样的任务应该做什么?将指针指向的地址内容复制到目的地(非指针)不是很直观.

另一方面,作为参考(现实,而不是假设):

a = b;//看起来不错,直观,因为现在两边都是同一种类型

参考,你有相同的类型,只有当 b 作为参数传递给 operator=() 时,它才通过引用传递(或者说通过指针,因为引用是指针的语法糖.)避免不必要的复制,这反过来又不会影响性能,因为它会如果 它是按值传递的.

值得注意的是,不仅 b 是通过 reference(或下面的 pointer)传递的,a 也通过指针传递给函数,因为我们知道在函数中,关键字this实际上是一个指针.>

所以 references 在 C++ 中被引入,让整个事情看起来对程序员来说很好和直观,否则它们是指针在下面.事实上,大多数编译器在内部使用指针(指针机制)来实现引用.

As per the comment under this answer, references were introduced primarily to support operator overloading which quotes Bjarne Stroustrup:

References were introduced primarily to support operator overloading. C passes every function argument by value, and where passing an object by value would be inefficient or inappropriate the user can pass a pointer. This strategy doesn’t work where operator overloading is used. In that case, notational convenience is essential so that a user cannot be expected to insert address− of operators if the objects are large.

Which implies that operator overloading can't work with pointer. But it doesn't clearly explain why operator overloading with pointers can't work. Why wouldn't operator overloading work for pointers?

IMO where references are used, pointers can also be used in its place.

解决方案

Because if it was allowed, then it would not look good, and wouldn't be as intuitive as its with reference.

Suppose it is allowed, then you would write:

struct A{};
A a, *pa, b;

a = pa ;//doesn't look good, also not intuitive. (not real C++)

It doesn't look good, because on left side you've non-pointer, on right side you've pointer. Looks very very weird. Also, since the types don't match, it doesn't look very intuitive as to what exactly its doing. I mean, you're assigning pointer to a non-pointer; what such an assignment is supposed to do? Copying the content of the address pointed to by pointer to the destination (non-pointer) is not very inttuitive.

On the other hand, as its allowed with reference (the reality, not a supposition):

a = b; //looks good, intuitive, as now both side is same type

With reference, you've both side same type, its only when b gets passed to operator=() as argument, it is passed by reference (or say by pointer, as references are syntactic sugar of pointers.) to avoid unnecessary copy, which in turn doesn't hinder performance, as it would if it is passed by value.

It would be also interesting to note that not only b is passed by reference (or pointer underneath), a also gets passed to the function by pointer, because we know in the function, the keyword this is actually a pointer.

So references were introduced in C++, to make whole thing look good and intuitive for programmers, otherwise they're pointers underneath. In fact, most compilers implement references using pointers (pointer-mechanism) internally.

这篇关于为什么不允许指针的运算符重载工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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