Ағылшыншамен салыстырыңыз: абзацты басыңыз — түпнұсқа терезеде ашылады. Абзац астындағы EN түймесі оны мәтін ішінде көрсетеді.
Кіріспе
Кейбір бағдарламалау тілдерінің мүмкіндіктері
Capability of some programming languages
Кейбір бағдарламалау тілдерінде функцияның немесе әдістің жүктелуі – бірдей атаумен, бірақ әртүрлі іске асырылулары бар бірнеше функцияны құру мүмкіндігі. Жүктелінген функцияға шақыру, шақыру контексіне сәйкес келетін осы функцияның нақты іске асырылуын іске қосады, бұл бір функция шақыруына контекстке байланысты әртүрлі міндеттерді орындауға мүмкіндік береді. Мысалы, және – жүктелінген функциялар. Екіншісін шақыру үшін объектіні параметр ретінде беру қажет, ал біріншісіне параметр қажет емес және бос параметрмен шақырылады. Көбінесе кездесетін қате – екінші функциядағы объектіге әдепкі мән беру, бұл компилятор қай әдісті пайдалану керектігін білмегендіктен, екіұшты шақыру қатесіне әкеледі. Тағы бір мысал – мәтінді немесе суреттерді басып шығаруға байланысты әртүрлі әрекеттерді орындайтын функция. Екі түрлі функцияны жүктеуге болады, егер біз бағдарламамыздың "басып шығаратын" барлық объектілері үшін жүктелінген басып шығару функциясын жазсақ, біз объектінің түрі туралы уайымдамаймыз, және дұрыс функция қайтадан шақырылады, шақыру әрқашан: .
In some programming languages, function overloading or method overloading is the ability to create multiple functions of the same name with different implementations. Calls to an overloaded function will run a specific implementation of that function appropriate to the context of the call, allowing one function call to perform different tasks depending on context. For example, and are overloaded functions. To call the latter, an object must be passed as a parameter, whereas the former does not require a parameter, and is called with an empty parameter field. A common error would be to assign a default value to the object in the second function, which would result in an ambiguous call error, as the compiler wouldn't know which of the two methods to use. Another example is a function that executes different actions based on whether it's printing text or photos. The two different functions may be overloaded as If we write the overloaded print functions for all objects our program will "print", we never have to worry about the type of the object, and the correct function call again, the call is always: .
Асқынулар
Функция жүктеуін қиындататын екі мәселе өзара әрекеттеседі: атаудың жасырылуы (ауқымға байланысты) және жасырын түрлендіру. Егер функция бір ауқымда жарияланса, содан кейін сол аты бар тағы бір функция ішкі ауқымда жарияланса, екі мүмкін жүктеу мінез-құлқы бар: ішкі жариялау сыртқы жариялауды (қолтаңбасына қарамастан) жасырады, немесе екі жариялау да жүктемеге қосылады, бірақ ішкі жариялау сыртқы жариялауды тек қолтаңба сәйкес келгенде ғана жасырады. C++ тілінде біріншісі қабылданған: "C++-та ауқымдар арасында жүктеме болмайды". Осыдан келіп, әртүрлі ауқымдарда жарияланған функциялардың жүктеме жиынтығын алу үшін сыртқы ауқымдағы функцияларды кілт сөз арқылы ішкі ауқымға нақты импорттау қажет. Жасырын түрлендіру функция жүктеуін қиындатады, себебі егер параметрлердің түрлері жүктелген функциялардың бірінің қолтаңбасына дәл сәйкес келмесе, бірақ түрлендіруден кейін сәйкес келсе, шешім таңдалған түрлендіруге байланысты болады. Бұл жағдайлар шатастыратын тәсілмен бірігуі мүмкін: мысалы, ішкі ауқымда жарияланған шамалы сәйкестік сыртқы ауқымда жарияланған нақты сәйкестікті жасыра алады.
Two issues interact with and complicate function overloading: Name masking (due to scope) and implicit type conversion. If a function is declared in one scope, and then another function with the same name is declared in an inner scope, there are two natural possible overloading behaviors: the inner declaration masks the outer declaration (regardless of signature), or both the inner declaration and the outer declaration are both included in the overload, with the inner declaration masking the outer declaration only if the signature matches. The first is taken in C++: "in C++, there is no overloading across scopes." As a result, to obtain an overload set with functions declared in different scopes, one needs to explicitly import the functions from the outer scope into the inner scope, with the keyword. Implicit type conversion complicates function overloading because if the types of parameters do not exactly match the signature of one of the overloaded functions, but can match after type conversion, resolution depends on which type conversion is chosen. These can combine in confusing ways: An inexact match declared in an inner scope can mask an exact match declared in an outer scope, for instance.