Tabbas! Zan yi farin cikin samar muku da labarin da ke da alaƙa da batun: 'Dawowar Aiki Mai Kyau a cikin C++'. Don haka, bari mu fara wannan tafiya:
C ++ harshe ne mai ƙarfi da ake amfani da shi don hadaddun tsarin da haɓaka wasan, amma kuma yana da wasu fasaloli na musamman waɗanda ke sanya shi ɗan rikitarwa, musamman ga masu farawa. Ɗayan irin wannan ra'ayi shine aikin da ke gyarawa da mayar da abubuwa masu daraja. Wannan ra'ayi na ayyukan dawo da kai na iya zama mai fa'ida sosai idan aka yi amfani da shi a cikin mahallin hanyar sarƙoƙi, haɓaka iya karantawa da ƙa'idodin lamba.
Fahimtar Ayyukan Maida Kai
Ainihin, ayyukan mayar da kai Siffar C ++ ce inda hanya ta dawo da misalin aji mai ɗaukar nauyi. Kyakkyawan hanyar aiwatar da wannan a cikin C++ ita ce ta dawo da batun abin da ake kiran aikin memba a kai ('*wannan').
class MyClass {
jama'a:
MyClass&gyara() {
// Lambar gyara anan
dawo *wannan;
}
};
A cikin wannan snippet na lambar, hanyar gyara() tana yin wasu gyare-gyare (wanda aka wakilta ta sharhin mai wurin) sannan ya dawo da batun abin da ake kira ('*wannan').
Yin nazarin Magani
Maganin farko ya dogara ne akan amfani da '*wannan' keyword. A cikin C++, 'wannan' kalma ce da ta ƙunshi mai nuni ga abin da ke kiran aikin. Idan muka yi watsi da ma'anar 'wannan' (watau '*wannan'), za mu sami ainihin abu, ba kawai mai nuni ba. Don haka, idan muka dawo '*wannan', muna mayar da abin da yake yanzu.
- Na farko, a cikin ajin MyClass, muna ayyana aikin jama'a gyara(). Wannan aikin zai ɗauki alhakin yin canje-canje ga ajin da dawo da ajin da aka sabunta.
- Jikin aikin gyare-gyare yana ƙunshe da lambar gyarawa, bayan haka za'a dawo da ''*wannan'. Wannan matakin yana dawo da abin da aka gyara yadda ya kamata.
Zurfafa nutsewa cikin Komawar Aiki
A cikin C++, nau'in dawowar aikin shine nau'in bayanai na ƙimar da aikin ke dawowa. Wasu ayyuka suna yin ayyukan da ake so ba tare da dawo da ƙima ba. A wannan yanayin, nau'in dawowa shine kalmar 'void'.
Koyaya, yawancin ayyuka suna yin ƙididdigewa sannan suna dawo da sakamakon takamaiman nau'in. Ayyukan da muka bayyana a baya yana dawo da tunani zuwa abu, wanda 'MyClass&' ke nunawa. Yana nuna nau'in bayanan da aikinmu zai dawo.
Amma, me yasa ya dawo da tunani? Mayar da tunani, maimakon abu, yana hana ƙarin kwafi kuma yana ba da yuwuwar haɓaka aiki. Idan muka mayar da abin da kansa, C++ zai gina sabon abu kuma ya kwafi duk bayanan daga ainihin abin. Wannan hanya na iya zama mai kyau ga ƙananan abubuwa amma ga waɗanda suka fi girma, zai iya rinjayar aikin lokacin gudu.
A cikin fahimtar yanayin salon salo, yi tunanin ayyukan dawo da kai a cikin C++ kamar yadda 'kallo mai launi' a cikin tufafi. Kamar yadda yanki ɗaya ke cika ɗayan don ƙirƙirar tasiri mai ban sha'awa, waɗannan nassoshi na abin da ke dawo da kai suna ba mu damar haɗa hanyoyin haɗin gwiwa don samar da mafi tsafta kuma mafi iya karanta lambar.
Ka tuna, fahimtar abubuwan da ke cikin yaren shirye-shiryen ku yana kama da ci gaba da yanayin salon: yana buƙatar nazari, fahimta da aiki amma yana da kyau a yi ƙoƙarin.