Video: Dab tsi yog qhov txawv ntawm virtual muaj nuj nqi thiab ntshiab virtual muaj nuj nqi hauv C ++?
2024 Tus sau: Lynn Donovan | [email protected]. Kawg hloov kho: 2023-12-15 23:47
Lub ntsiab qhov sib txawv ntawm ' virtual function' thiab 'ntshiab virtual muaj nuj nqi ' yog ' virtual muaj nuj nqi ' muaj nws txhais hauv cov chav kawm hauv paus thiab cov chav kawm qub txeeg qub teg tau rov txhais nws. Cov ntshiab virtual muaj nuj nqi tsis muaj kev txhais hauv cov chav kawm hauv paus, thiab tag nrho cov chav kawm tau txais txiaj ntsig tau txais yuav tsum rov txhais nws.
Nyob rau hauv txoj kev no, dab tsi yog qhov ntshiab virtual muaj nuj nqi C ++?
Ntshiab Virtual Functions thiab Abstract Classes hauv C ++ Peb tsis tuaj yeem tsim cov khoom ntawm cov chav kawm paub daws teeb meem. A ntshiab virtual muaj nuj nqi (los yog abstract muaj nuj nqi ) hauv C++ yog a virtual muaj nuj nqi uas peb tsis muaj kev siv, peb tsuas tshaj tawm nws. A ntshiab virtual muaj nuj nqi tau tshaj tawm los ntawm kev muab 0 hauv kev tshaj tawm.
Ib sab saum toj no, dab tsi yog kev ua haujlwm virtual thiab chav kawm virtual? A virtual muaj nuj nqi yog ib tug tswv cuab muaj nuj nqi nyob rau hauv lub hauv paus chav kawm uas peb redefine nyob rau hauv ib tug derived chav kawm . Nws tau tshaj tawm tias siv lub virtual lo lus tseem ceeb. Thaum a chav kawm muaj virtual muaj nuj nqi yog inherited, tus derived chav kawm redefine cov virtual muaj nuj nqi kom haum nws tus kheej xav tau.
Nyob rau hauv no hais txog, dab tsi yog virtual thiab ntshiab virtual function piav nrog piv txwv?
A ntshiab virtual muaj nuj nqi yog a muaj nuj nqi uas yuav tsum tau overridden nyob rau hauv ib chav kawm derived thiab tsis tas yuav txhais . A virtual muaj nuj nqi tau tshaj tawm tias yog dawb huv ” siv cov xav paub = 0 syntax. Rau piv txwv : class Base {
Dab tsi yog kev siv cov haujlwm virtual?
Cov haujlwm virtual xyuas kom meej tias qhov tseeb muaj nuj nqi yog hu rau ib yam khoom, tsis hais hom kev siv (los yog pointer) siv rau muaj nuj nqi hu. Muaj nuj nqi tau tshaj tawm nrog a virtual lo lus tseem ceeb hauv chav kawm hauv paus. Qhov kev daws teeb meem ntawm muaj nuj nqi hu tau ua thaum Run-time.