Dab tsi yog txoj kev ntshiab virtual?
Dab tsi yog txoj kev ntshiab virtual?

Video: Dab tsi yog txoj kev ntshiab virtual?

Video: Dab tsi yog txoj kev ntshiab virtual?
Video: Neeg txoj kev ntshaw yog dab tsi tiag 2024, Kaum ib hlis
Anonim

A ntshiab virtual muaj nuj nqi los yog txoj kev ntshiab virtual yog a virtual muaj nuj nqi uas yuav tsum tau ua los ntawm chav kawm derived yog hais tias chav kawm derived tsis yog abstract. Cov chav kawm uas muaj cov txheej txheem virtual yog hu ua "abstract" thiab lawv tsis tuaj yeem instantiated ncaj qha.

Piv txwv li, dab tsi yog qhov kev ua haujlwm zoo virtual?

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 tsum tau txhais. A virtual muaj nuj nqi tau tshaj tawm tias yog dawb huv ” siv qhov xav paub = 0 syntax . Rau piv txwv : class Base {

Ib tug kuj yuav nug, dab tsi yog qhov txawv ntawm virtual thiab ntshiab virtual muaj nuj nqi? Lub ntsiab qhov sib txawv ntawm ' virtual muaj nuj nqi '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.

Ib sab ntawm qhov no, puas tuaj yeem ua haujlwm dawb huv muaj kev txhais?

Yog, a ntshiab virtual muaj nuj nqi tuaj yeem muaj ib cev. Tag nrho dawb huv virtual txhais tau tias yog koj ua tau tsis hu lub muaj nuj nqi siv ib yam khoom ntawd muaj tshaj tawm los yog muaj inherited cov ntshiab virtual muaj nuj nqi . Vim li no, koj tsis tuaj yeem tsim cov khoom ntawm cov chav kawm nrog ntshiab virtual functions.

Dab tsi yog kev ua haujlwm virtual thiab kev ua haujlwm dawb huv hauv C ++?

A ntshiab virtual muaj nuj nqi yog a virtual muaj nuj nqi hauv C ++ uas peb yuav tsum tsis txhob sau ib yam muaj nuj nqi txhais thiab tsuas yog peb yuav tsum tshaj tawm nws. Nws tau tshaj tawm los ntawm kev muab 0 hauv kev tshaj tawm. Ib chav kawm abstract yog chav kawm hauv C++ uas muaj tsawg kawg yog ib qho ntshiab virtual muaj nuj nqi.

Pom zoo: