virtual function specifier
Da cppreference.com.
![]() | Questa pagina è stata tradotta in modo automatico dalla versione in ineglese della wiki usando Google Translate. La traduzione potrebbe contenere errori e termini strani. Muovi il puntatore sopra al testo per vedere la versione originale. Puoi aiutarci a correggere gli gli errori. Per ulteriori istruzioni clicca qui. |
Specifica che una funzione è virtuale
Original:
Specifies that a function is virtual
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Indice |
[modifica]Sintassi
virtual function_declaration; | |||||||||
[modifica]Spiegazione
Funzioni virtuali sono funzioni membro il cui comportamento può essere sottoposto a override in classi derivate. Al contrario di non-virtuali funzioni, il comportamento override è preservata anche se non vi è alcuna fase di compilazione informazioni sul tipo effettivo della classe. Ciò significa che, anche se una classe derivata sono trattati con puntatore o un riferimento alla classe base, una chiamata a una funzione di override virtuale sarebbe invocare il comportamento definito nella classe derivata.
Original:
Virtual functions are member functions whose behavior can be overridden in derived classes. As opposed to non-virtual functions, the overridden behavior is preserved even if there is no compile-time information about the actual type of the class. That means, even if a derived class is handled using pointer or reference to the base class, a call to a overridden virtual function would invoke the behavior defined in the derived class.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
La firma della funzione deve essere lo stesso, al fine di essere sottoposto a override.
This section is incomplete Reason: handling of function signatures (member lookup rules) |
Original:
The function signature must be the same in order to be overridden.
This section is incomplete Reason: handling of function signatures (member lookup rules) |
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Il tipo di ritorno di una funzione prevalente virtuale non deve necessariamente essere identica a quella della funzione override. I tipi possono essere diversi se sono' covariante con ogni altro. Due tipi sono covarianti se soddisfano i seguenti requisiti:
Original:
The return type of a overriding virtual function doesn't necessarily need to be the identical to that of the overridden function. The types can be different if they are covariant with each another. Two types are covariant if they satisfy the following requirements:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
- Qui ci si riferisce alla funzione prevalente come
Derived::f()
e alla funzione override comeBase::f()
Original:Here we refer to the overriding function asDerived::f()
and to the overridden function asBase::f()
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- entrambi i tipi sono puntatori o riferimenti alle classi. Multi-livello di puntatori o riferimenti non sono ammessi.Original:both types are pointers or references to classes. Multi-level pointers or references are not allowed.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - la classe del tipo di ritorno
Base::f()
deve essere una classe univoca e accessibili base diretta o indiretta della classe del tipo di ritorno diDerived::f()
.Original:the class of the return type ofBase::f()
must be a unambiguous and accessible direct or indirect base class of the class of the return type ofDerived::f()
.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - il tipo di ritorno di
Derived::f()
deve essere cv-qualificato ugualmente o meno il tipo di ritorno diBase::f()
.Original:the return type ofDerived::f()
must be equally or less cv-qualificato than the return type ofBase::f()
.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Se una funzione virtuale viene chiamato direttamente, cioè qualificando esplicitamente la classe è un membro, allora il meccanismo di chiamata virtuale viene soppressa e che l'attuazione particolare è chiamato.
Original:
If a virtual function is called directly, that is, explicitly qualifying the class it is a member of, then the virtual call mechanism is suppressed and that particular implementation is called.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Un distruttore virtuale di una classe base è sempre sottoposto a override da un distruttore di una classe derivata, anche se che i distruttori non sono altrimenti ereditate.
Original:
A virtual destructor of a base class is always overridden by a destructor of a derived class, even though that destructors are otherwise not inherited.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Le regole di accesso a una funzione virtuale sono determinati dalla prima dichiarazione. Le regole di accesso definite dalle dichiarazioni delle funzioni imperative si applicano solo alle chiamate di funzione dirette.
Original:
The access rules to a virtual function are determined by the first declaration. Access rules defined by the declarations of the overriding functions apply only to the direct function calls.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
virtual
funzione di identificatore di appartenenza implica, quindi solo funzioni membro può essere virtuale. Inoltre, poiché un'istanza di una classe è necessario per chiamare una funzione virtuale, funzione virtuale non può essere static.Original:
virtual
function specifier implies membership, thus only member functions can be virtual. Also, since an instance of a class is needed in order to call a virtual function, virtual function can not be static.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Modelli di funzioni non possono essere dichiarate
virtual
. Questo vale solo per le funzioni che sono loro stessi modelli - una funzione di membro regolare di un modello di classe possono essere dichiarati virtuali.Original:
Functions templates cannot be declared
virtual
. This applies only to functions that are themselves templates - a regular member function of a class template can be declared virtual.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[modifica]Esempio
class Parent {public:void functionA();virtualvoid functionB();//Note the keyword virtualvoid functionC();}; class Child :public Parent {public:void functionA();virtualvoid functionB();//Note the keyword virtual}; int main(){ Parent* p1 = new Parent; Parent* p2 = new Child; Child* c = new Child; p1->functionA();//Calls Parent::functionA p1->functionB();//Calls Parent::functionB p1->functionC();//Calls Parent::functionC p2->functionA();//Calls Parent::functionA because p2 points to a Parent p2->functionB();//Calls Child::functionB even though p2 points // to a Parent because functionB is virtual p2->functionC();//Calls Parent::functionC c->functionA();//Calls Child::functionA c->functionB();//Calls Child::functionB c->functionC();//Calls Parent::functionC return0;}
[modifica]Vedi anche
- ignorare specificatore(dal C++11)
- identificatore finale(dal C++11)