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?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
-
What is the difference between
and in java - How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
To make the expression
a = b;
compile you need to either have anoperator=
in the type ofa
that takes an element of the type ofb
, or a type implicitly convertible fromb
.The first case is ruled out, since
operator=
must be a member of the class, and since you cannot modifyGLPoint
then you cannot addGLPoint& GLPoint::operator=( GLSize )
.The second case suffers the same type of problems. An implicit conversion from
GLSize
toGLPoint
can be implemented as an implicit constructor inGLPoint
(ruled out), or as a memberoperator GLPoint()
inGLSize
, which requires modification ofGLSize
. Conversions cannot be added as free functions either.The alternatives are using non-operator syntax, as adding a free function
assign
(orcopy
):GLPoint& assign( GLPoint&, GLSize const & )
.The next question is why would you want to do so. If the designers of
GLPoint
andGLSize
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
toGLPoint
, you might by mistake type something like:distance( point1, size2 )
where you meantdistance( 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)If you can derive from or wrap CGPoint and use the new class instead throughout your code, then you can provide whatever operators you like. The new class can have a conversion operator to
CGPoint
facilitating interaction with existing functions.Other answers seams to miss the obvious solution : add a function to convert CGPoint into CGSize. Off course, that is not exactly what you want (
size = point
), but since you can not modify either of two classes, this is the only way :When you assign a
CGSize
to aCGPoint
- what happens? Distil that into some operator and there you have it - for exampleWhat's so difficult about this? Here is an example: http://www.ideone.com/FZN20