Author: Solmyr Form pchome.net
"Why is this ?!", Zero is muttered while drinking water, hate the code on the display in front of the display, "Why is a simple call to compilers ..." "" "This is because your design is too Poor! "Hey! Zero appeared in the back of the ghost, SolmyR scared a big jump, a sided water difference point out. "Cough! Cough! S ... Solmyr, when are you standing behind me?" Zero is very laborious to cough, and try to recall if he has just caught it by Solmyr. SolmyR grabbed a chair and sat down: "I am in the first time, I am here, it is this bad design leads to it now, it is now bothering your compilation error." "Where?" "" Here "Solmyr grabbed the keyboard and marked the following code: void Somefunc (INT i) ............ void somefunc (float f) ............ int main () { ......... SomeFunc 1.2); } "I also feel strange", ZERO, as always, to scratch the head, try to crush the wisdom of absence, "So simple functions It should be clearly correct. I will be clearly shown here. It is clear that the floating point number should be called. The most strange thing is that if there is no such call, the entire program is completely no problem, it can be seen such an overload function It is legal. "" Well, yes, it is really legal, but legal does not represent correct. Zero, you read this paragraph, look at the prophet Meyers in his "50" (Note: "Effective C 2 / The clause 26 in the e "one book) describes how C treats' ambigulent philosophy." SolmyR opened a book and pointed to the one. "C ..." "Stand up, big voice!" ZERO stood up, the middle is full of attention: "C also has a philosophical belief: it believes that the potential ambiguous two can be not a mistake." Upload next to the seat upload Low, smiling, people who have a further visit to the probes, cast curiosity, Zero suddenly feels yourself like a fool. When Zero saw SolmyR's signboard, it understood: I was designed again by SolmyR again. "Well, I understand this, we can make further discussions", SolmyR started to transfer to the topic, "Remember the last 1.2?" Zero reveals the expression of memories: "Well ..." ... 1.2 is the constant 'written in the code' ... should be a Double type constant. "" This is the problem. "The compiler sees the request for this calling function. It will look for which function in your overload function can match. This call request gives the parameters, and it is found that the parameters without a function are Double type, so you must do type conversion, but the double type can be converted into int, or it can be converted to float, which is better? Compile I don't know, so I have to report an error. Do you understand? "Zero nodded.
"Then I ask you, will you report wrong when you overload compile?" SolmyR changed a little bit of Zero code: Void Somefunc (INT i) ............ Void Somefunc (Double DB) ............ int main ) { ......... .. The parameter is a float type, so you must do type conversion, but the float type can be converted to int, or it can be converted to Double. Which is good? The compiler does not know, so there is no mistake. "" Wrong! ", SolmyR Put the run button in hand, and the program runs everything, the output display is called the SomeFunc function of the Double version. ZERO is again confused: "Why is it necessary to choose type conversion, this is right, what is wrong? What is the logic of this?" "This sentence:" Which is good? Compiler I don't know '. You didn't pay attention to what I said when I said this sentence? "" Do you use the stress? "" ... This is not a focus. Focus is, float to int and float To double conversions, the compiler is capable of choosing, because float to int will lose data - the image-like compiler will give a Warning when doing this type of conversion - and Float to Double is not lost Data, so the compiler knows 'Turn which is better. The previous situation, Double, the conversion of Double to INT to FLOAT will lose data, so the compiler doesn't know which better', it can't make a decision - ", Solmyr looked at ZERO, then asked again, "Do you understand?" Zero wrinkled, scratching more stronger, obviously feeling a little difficult to digest the so many information appeared at once: "I think I understand, the key Is the compiler to distinguish between two types of conversion. The key to distinguish here is whether the type conversion loses data, um ... so I use the double type in all the number of floats, there will be no problem, even if others The same is also called with float. "" Correct. But 'ambiguous two "problems can not just float, such as two overload functions ...", Solmyr then type: void Somefunc (double db) Void Somefunc (charr) CH) "If I use a plastic variable to call, what will happen?", SolmyR twist stared at Zero. "Oh ... The compiler also cannot distinguish between INT to Double and INT to Char, it will also report an error." "Correct. Can you give yourself a few examples?", SolmyR handed back the keyboard. Obviously, Zero has fallen into meditation, after a while, there is such a few lines of code on the screen: // If the int call, the void fun (char ch) Void Fun (int * pi) or other Pointer // Use int calls the same error VOID FUN (Double DB) Void Fun (INT * PI)
But you still miss an important situation, ", SolmyR added," is when the parameter has a default value: "// If you do not give parameters, if you do not give parameters, Void Fun (INT i = 10) Void Fun ()" God! ", Zero looks soon to collapse," there is so much a multi-ambiguous trap, this told me how to release my function? " Write in the document: The following 153 modifiers will cause compilation errors? "Don't be so nervous,", SolmyR said, "The ambiguity of the heavy-duty function is not avoidable, there are two ways: one is to use template instead of heavy load, especially like your Somefunc," INT type, " The Double type processing algorithm is the same; the second is to ensure that the number of parameters of the function is different if you want to use overload. "But if the processing algorithm is different, the number of parameters required by the function is the same, what should I do? "" It's very simple, join the 'useless parameter', like this: "Void SomeFunc (Float DB, int) Void Somefunc (INT i)" The second parameter of the first function does not have any effect, so you can simply give It is named, as long as you declare this int type parameter. You can write this way in the document: This parameter is to transfer 0 values when you are reserved in the future. "" ... It's probably this type in your document ... ah! it hurts! This is another book! ", Zero was hit by SolmyR, and sent a tragic sorrow." You have to thank SCOTT Meyers, his "50" is light and thin, and if you take it in your hand, if you are a "Bible" of Bjarne Stroustrup (Note: Refers to the book "The C Programing Language 3 / E", Bjarne Stroustrup is a C language designer), you can't climb it now. "SolmyR once again put on the cultivated camouflage, but the words still left a little murderous traces ..." It's a brutal guy ... ", Zero whispered." What do you say? " ", Murderous rise again." No, no! I didn't say anything! ", ZERO is busy and denied, try to transfer the topic," Ah! I understand, to avoid the ambiguous two-can be trap, one is to replace the overload using the template, and the other is to use the added "useless parameter 'to ensure the number of overload function parameters. This will avoid the ambiguous problem, is it, SolmyR? ". Zero is very hard to load the innocent look." ... is really poor actress ... ", SolmyR's heart is in mind.