Yuav rho tawm haujlwm li cas hauv C ++?
Yuav rho tawm haujlwm li cas hauv C ++?

Video: Yuav rho tawm haujlwm li cas hauv C ++?

Video: Yuav rho tawm haujlwm li cas hauv C ++?
Video: Muaj Yis hauv Xib tes yuav Qhia tau Txoj Hmoo li cas 2024, Tej zaum
Anonim

Thaum twg rho tawm yog siv los deallocate nco rau a C++ class object, tus object destructor yog hu ua ntej qhov khoom nco yog deallocated (yog hais tias tus kwv hasa destructor). Yog hais tias tus operand rau lub rho tawm tus neeg ua haujlwm yog ib tug modifiable l-tus nqi, nws tus nqi yog undefined after the object yog deleted.

Cov lus nug tseem yog, qhov sib txawv ntawm kev rho tawm thiab rho tawm hauv C ++ yog dab tsi?

Ob leeg siv rau tib lub hom phiaj, tab sis tseem muaj qee qhov sib txawv , cov sib txawv yog: rho tawm yog anoperator whereas free() yog lub tsev qiv ntawv muaj nuj nqi. rho tawm freethe allocated nco thiab hu destructor. Tab sis free() de-allocatememory tab sis tsis hu destructor.

Ib tug kuj yuav nug, C ++ tshiab ua haujlwm li cas? Thaum twg tshiab yog siv los faib kev nco rau a C++ class object, tus kwv tus constructor hu ua afterthe memory is allocated. Thaum allocating ib array siv lub tshiab tus neeg teb xov tooj, thawj qhov loj me tuaj yeem yog xoom - tus tshiab tus neeg teb xov tooj rov qab tus pointer tshwj xeeb.

Ib tug kuj yuav nug, dab tsi yog kev siv tus tshiab thiab rho tawm tus neeg teb xov tooj hauv C ++?

C++ txhawb dynamic faib thiab deallocation ntawm cov khoom siv lub tshiab thiab rho tawm tus neeg teb xov tooj . Cov no cov neeg ua haujlwm faib lub cim xeeb rau cov khoom los ntawm lub pas dej ua ke hu ua thefree store. Cov tus neeg ua haujlwm tshiab hu rau qhov tshwj xeeb muaj nuj nqi tus neeg ua haujlwm tshiab , thiab cov rho tawm tus neeg teb xov tooj hu rau cov haujlwm tshwj xeeb tus neeg teb xov tooj rho tawm.

Kuv puas yuav tsum rho tawm pointers C ++?

1 Teb. Koj tsis ua yuav tsum rho tawm nws, thiab, ntxiv, koj yuav tsum tsis txhob rho tawm nws. Yog hais tias lub ntiaj teb yog ib qho khoom siv tsis siv neeg, nws yuav raug tso tawm kom raug. Yog li ntawm manually rho tawm a taw tes rau nws, koj mus rau hauv undefined behavior.