C ++操作符重载指针 [英] C++ operator overloading for pointers

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

问题描述

我不知道(只是出于好奇)为什么操作符重载是不允许在C ++的指针。我的意思是这样的:

I wonder (just out of curiosity) why operator overloading isn't allowed in C++ for pointers. I mean something like this:

Vector2d* operator+(Vector2d* a, Vector2d* b) { return new Vector2d(a.x + b.x, a.y + b.y); }

Vector2d* a = new Vector2d(1, 1); 
Vector2d* b = new Vector2d(2, 2); 
Vector2d* c = a + b; 

请注意'a + b'如何创建一个新的Vector对象, c',而不调用复制构造函数。所以它会解决新的右值引用解决的同样的问题。而且,据我所知,它几乎等同于在C#中使用运算符重载时发生的情况(但我可能在这里错了,我从来没有使用过C#),以及为什么在C#中Rvalue refs不是必要的。

Note how 'a + b' creates a new Vector object, but then copies only its address into 'c', without calling a copy constructor. So it would sort of solve the same problem that the new rvalue references solve. Also, as far as I know, it's pretty much equivalent to what happens when using operator overloading in C# (but I might be wrong here, I've never actually used C#), and why rvalue refs are not necessary in C#.

真的,右值引用解决方案甚至更好,因为它允许基于堆栈的对象,而这种重载会迫使所有Vector2d对象存在堆上,它似乎是一些很容易在编译器中实现的东西,可能在右值引用发生前的几年。使用自定义分配器,它甚至不会

True, the rvalue reference solution is even better, as that allows stack-based objects, while this overloading would force all Vector2d objects to live on the heap, but still, it seems to be something that would have been easy to implement in compilers, possibly years before rvalue refs came around. And with custom allocators, it wouldn't even be that slow.

这只是非法的,因为最少的惊喜原则,还是有其他原因吗?

So is this only illegal because of the "least surprise" principle, or are there other reasons too?

推荐答案

真的,右值引用解决方案更好,因为它允许基于堆栈的对象,而这个重载将迫使所有的Vector2d对象在堆上生活,但是,似乎是一些本来很容易在编译器中实现的东西,可能在rvalue refs之前的几年。和自定义分配器,它甚至不会那么慢。

True, the rvalue reference solution is even better, as that allows stack-based objects, while this overloading would force all Vector2d objects to live on the heap, but still, it seems to be something that would have been easy to implement in compilers, possibly years before rvalue refs came around. And with custom allocators, it wouldn't even be that slow.

这只是非法的,因为最小惊喜的原则,还是有其他原因吗?

So is this only illegal because of the "least surprise" principle, or are there other reasons too?




  • 它不正确链接...什么是 a + b + c

  • 指针运算在C ++中已经有意义了...对于跨类型一致性有用,算法无法在对象的容器上正常工作。

  • 按值返回值意味着对象不需要调用者清除:如果它使用自由存储(堆)存储实际数据,它会根据需要自动删除它。

    • It doesn't chain properly... what's a + b + c supposed to do, leak memory?
    • Pointer arithmetic already has a meaning in C++... it's useful for that to be consistent across types, otherwise e.g. algorithms wouldn't work properly on containers of your objects.
    • Returning something by value implies that object doesn't need clean up by the caller: if it uses free store (heap) to store actual data, it'll be deleting it automatically as necessary. Makes consistently robust memory usage easier.

    • 请注意'a + b'如何创建一个新的Vector对象,但是然后仅将其地址复制到c中,而不调用复制构造函数。因此,它将解决与新的右值引用解决的相同的问题。

      Note how 'a + b' creates a new Vector object, but then copies only its address into 'c', without calling a copy constructor. So it would sort of solve the same problem that the new rvalue references solve.

      通常实现的返回值优化也解决了这个问题,帮助编译器安排将返回值直接构造到调用者的缓冲区中。

      The commonly implemented Return Value Optimisation also addressed this problem, helping the compiler arrange for construction of the return value directly into the caller's buffer.

      这篇关于C ++操作符重载指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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