是否可以在结构体之外编写自动转换运算符? [英] Is it possible to write auto-cast operator outside a struct?

查看:99
本文介绍了是否可以在结构体之外编写自动转换运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是确切的情况:
我在系统API结构中定义了 CGPoint CGSize 我想能够写 my_point = my_size
我不能修改 CGPoint struct,只能写外部操作符。我可以写二进制运算符( + - ,...),但 operator = / code>必须通过在struct中声明。 c> a = b; c $ c>编译你需要在 a 类型中有一个 operator = b ,或者一种可以从 b 隐式转换的类型。



第一种情况被排除,因为 operator = 必须是类的成员,并且因为您不能修改 GLPoint 那么你不能添加 GLPoint& GLPoint :: operator =(GLSize)



第二种情况遇到相同类型的问题。从 GLSize GLPoint 的隐式转换可以实现为 GLPoint (排除)或在 GLSize 中的成员运算符GLPoint() GLSize



替代方法使用非运算符语法,添加一个自由函数 assign (或 copy ): GLPoint&下一个问题是你为什么要这么做。下面的问题是你为什么要这么做。如果 GLPoint GLSize 的设计者没有考虑到一个大小应该分配给一个点,那么你为什么觉得他们应该是可转让的?通常,保持类型分离是一个好主意,因为这将使编译器能够检测您在代码中可能犯的错误。



如果允许从 GLSize GLPoint ,你可能会错误地输入类似: distance(point1,size2)其中你的意思是 distance(point1,point2) ,并且因为有一个转换,编译器会乐意转换和应用。然后,您会看到奇怪的结果,您将花费相当多的好的调试时间来确定逻辑错误的位置。



域对每个运算符在该上下文中的含义有非常清晰的定义,我会避免运算符在所有代价重载。 读取您的代码将立即理解 GLPoint(1,2)+ GLSize(5)代表毫无疑问或歧义?如果不是这样,如果人们会感到惊讶甚至怀疑,那么避免操作符重载并使用命名函数: move_up(GLPoint& GLSize)尺寸意味着你)


The exact situation is next: I have defined in system API structs CGPoint and CGSize, and I want to be able to write my_point = my_size. I can't modify CGPoint struct, only can write external operator. I can write binary operators (+, -, ...) but operator= must by declared inside struct. So is there any other solution?

解决方案

To make the expression a = b; compile you need to either have an operator= in the type of a that takes an element of the type of b, or a type implicitly convertible from b.

The first case is ruled out, since operator= must be a member of the class, and since you cannot modify GLPoint then you cannot add GLPoint& GLPoint::operator=( GLSize ).

The second case suffers the same type of problems. An implicit conversion from GLSize to GLPoint can be implemented as an implicit constructor in GLPoint (ruled out), or as a member operator GLPoint() in GLSize, which requires modification of GLSize. Conversions cannot be added as free functions either.

The alternatives are using non-operator syntax, as adding a free function assign (or copy): GLPoint& assign( GLPoint&, GLSize const & ).

The next question is why would you want to do so. If the designers of GLPoint and GLSize did not consider that a size should be assignable to a point, then why do you feel that they should be assignable? In general it is a good idea to keep types separate, as that will enable the compiler to detect mistakes you might make in your code.

If you allow implicit conversions from GLSize to GLPoint, you might by mistake type something like: distance( point1, size2 ) where you meant distance( point1, point2 ), and because there is a conversion, the compiler will gladly convert and apply. Then you will see strange results, and you will spend quite a few nice debugging hours trying to determine where the logic is wrong.

Unless the domain has a very clear definition of what each operator means in that context, I would avoid operator overloading at all costs. Will everyone reading your code immediately understand what GLPoint(1,2) + GLSize(5) represents without any doubt or ambiguity? If that is not the case, if people will be surprised or even doubt, then avoid operator overloading and use named functions: move_up( GLPoint&, GLSize ) (or whatever point+size means to you)

这篇关于是否可以在结构体之外编写自动转换运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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