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

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

问题描述

根据此答案下的注释,引入引用主要是为了支持运算符重载引用Bjarne Stroustrup:


引用主要是
操作符重载。 C
将每个函数参数传递给
值,并且通过
传递对象的效率低或者
不合适,用户可以传递一个
指针。这个策略不工作
使用了操作符重载。在
的情况下,符号的便利是
必要的,所以如果对象是大的,用户不能是
预期插入
操作符的地址。


这意味着操作符重载不能使用指针。但它没有清楚地解释为什么操作符重载用指针不能工作。为什么操作符重载不能用于指针?



IMO使用引用,指针也可以在其位置使用。



假设它是允许的,那么你会写:

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

a = pa; //看起来不好,也不直观。 (不是真正的C ++)

看起来不太好,指针,在右边你有指针。看起来很奇怪。此外,由于类型不匹配,它看起来不直观,因为它究竟是什么。我的意思是,你把指针指向一个非指针;这样的任务应该做什么?将指针指向的地址的内容复制到目标(非指针)并不是非常直观。



另一方面,现实,不是假设):

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

只有当 b 被传递给 operator =()作为参数时,它才会被传递通过引用(或通过指针说出来,因为引用是指针的语法糖。)避免不必要的复制,这不会妨碍性能,因为 / strong>,则会按值传递



这也是有趣的,注意不仅 b 通过 (或下面的指针), a 也会通过指针传递给函数,因为我们知道函数,关键字 c>

实际上是一个指针。



em>在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天全站免登陆