c++: Operator overloading and error handling -
I am currently beginning operator overloading in C ++ for simple 2D top class classes where [Available] Operator. It usually works, but I do not really know how to deal with errors for example if the operator is out of range (in the case of 2D top class in which there are only x and y values, then this range
itemprop = "text">
P> The time to handle the error is a difficult animal at the best time. It is too much a big deal error, and if this happens then it is a lot more likely to happen with it.
There are four basic paths you can follow:
- Throw an exception
- Error handling sledgehammer. A great tool, of course, is to use it if you need it, but if you are not careful then you will end up plucking yourself in the leg.
- Basically
throw
- If it has not been caught, it will cancel your program.
- Return a value indicating failure
- Leave it to check for success and respond accordingly.
- The failure value will depend on the type. Indentor can return
NULL
or0
, STL container backobject.end ()
, otherwise unused values can be used (like That-1
or""
)
- Process this situation well
- Sometimes, an error is not actually an error, it is just an inconvenience.
- If useful results can still be made available, without any injury to anyone, the fault of the fault is easily flowing under the carpet.
- For example, a mistake outside the limits can return the last variable in an array, without the need to resort to any dirty exceptions without resorting to it.
- Unless it is predicted and defined , programmers can make it as intended.
- Undefined behavior
- O, the programmer should not give you poor input in the first place. Make them suffer.
In general, for those things which I am not really expecting, only resort to the substitute without any effort Otherwise, the use of exceptions as flow control is slightly better than going back in the days of goto
.
The option is probably the most common for non-program-breakdown errors, but this effectiveness really depends on the type of return you are working on. This is beneficial because it allows the programmer to control the flow locally by locating and fixing failures. While working with overloading operators, this is of limited use, but I thought I would remove it for fullness.
The option three are very circumstantial-many errors can not be controlled in such a way, and even those who have unreliable results. Experiment with caution, and make sure to document well or, do not document it at all and pretend it to four options.
Now, as per the specific example given, this is overloaded on the overloaded operator with the limit error []
, I personally would not go for the option four Because I enjoy seeing other programmers in particular, when they have to deal with my code (I accidentally, but it tangents to the discussion), but because it is expected.
In most cases where a programmer operator []
, they expect to check their own limits and do not depend on the type or class to do anything for them. Even in STL containers, you operator []
(no limit check) otherwise redundant object.at ()
(which does range investigation) reflecting the expected behavior with your overloaded operators, more intuitive knowledge If appropriate create code.
Comments
Post a Comment