Espaços nominais
Variantes
Acções

virtual function specifier

Da cppreference.com
< cpp‎ | language

 
 
Linguagem C++
Tópicos gerais
Original:
General topics
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Controle de fluxo
Original:
Flow control
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Declarações execução condicional
Original:
Conditional execution statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Instruções de iteração
Original:
Iteration statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Ir declarações
Original:
Jump statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Funções
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
declaração da função
lambda declaração da função
modelo de função
linha especificador
especificações de exceção (obsoleta)
noexcept especificador (C++11)
Exceções
Original:
Exceptions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Namespaces
Original:
Namespaces
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Tipos
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
decltype specifier (C++11)
Especificadores
Original:
Specifiers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
cv especificadores
armazenamento duração especificadores
constexpr especificador (C++11)
auto especificador (C++11)
alignas especificador (C++11)
Inicialização
Original:
Initialization
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Literais
Original:
Literals
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Expressões
Original:
Expressions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
representações alternativas
Utilitários
Original:
Utilities
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Tipos
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
typedef declaration
tipo de alias declaração (C++11)
atributos (C++11)
Conversões
Original:
Casts
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
conversões implícitas
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
Elenco C-estilo e funcional
Alocação de memória
Original:
Memory allocation
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Classes
Original:
Classes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Classe propriedades específicas de função
Original:
Class-specific function properties
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
função virtual
substituir especificador (C++11)
final de especificador (C++11)
Funções membro especiais
Original:
Special member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Modelos
Original:
Templates
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
modelo de classe
modelo de função
especialização de modelo
pacotes de parâmetros (C++11)
Diversos
Original:
Miscellaneous
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Assembly embutido
 
Especifica que uma função é virtual
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.

Índice

[editar] Sintaxe

virtual function_declaration ;

[editar] Explicação

Funções virtuais são funções membro cujo comportamento pode ser substituído em classes derivadas. Ao contrário de não-virtuais funções, o comportamento substituído é preservada mesmo que não há informações de tempo de compilação sobre o tipo real da classe. Isso significa que, mesmo se uma classe derivada é tratado usando ponteiro ou referência para a classe base, uma chamada para uma função virtual seria substituído invocar o comportamento definido na classe derivada.
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.
A assinatura da função deve ser o mesmo, a fim de ser substituído.
Original:
The function signature must be the same in order to be overridden.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
O tipo de retorno de uma função primordial virtual não precisa ser necessariamente idêntico ao da função substituída. Os tipos podem ser diferentes se eles são' covariante um com o outro. Dois tipos são covariantes se satisfizerem os seguintes requisitos:
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.
Aqui nos referimos à função primordial como Derived::f() e para a função de substituído como Base::f()
Original:
Here we refer to the overriding function as Derived::f() and to the overridden function as Base::f()
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • ambos os tipos são ponteiros ou referências a classes. Multi-nível ponteiros ou referências não são permitidos.
    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.
  • a classe do tipo de retorno de Base::f() deve ser uma classe base inequívoca e acessível direta ou indireta da classe do tipo de retorno de Derived::f().
    Original:
    the class of the return type of Base::f() must be a unambiguous and accessible direct or indirect base class of the class of the return type of Derived::f().
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • o tipo de retorno deve ser Derived::f() cv-qualificados igual ou menor do que o tipo de retorno de Base::f().
    Original:
    the return type of Derived::f() must be equally or less cv-qualificados than the return type of Base::f().
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
Se uma função virtual é chamado diretamente, ou seja, qualificar explicitamente a classe é um membro, então o mecanismo de atendimento virtual é reprimida e que a implementação particular é chamado.
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.
Um destrutor virtual de uma classe base é sempre substituído por um destruidor de uma classe derivada, embora que de outra forma não destruidores são herdados.
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.
As regras de acesso a uma função virtual são determinados pela primeira declaração. Regras de acesso definidas pelas declarações das funções de substituição aplicam-se apenas às chamadas de funções diretas.
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.
virtual função especificador implica adesão, assim, apenas as funções de membro pode ser virtual. Além disso, uma vez que uma instância de uma classe é necessária, a fim de chamar uma função virtual, função virtual não pode ser 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.
Modelos de funções não pode ser declarada virtual. Isto aplica-se apenas às funções que são eles próprios modelos - uma função de membro regular de um modelo de classe podem ser declarados virtuais.
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.

[editar] Exemplo

class Parent {
public:
    void functionA();
    virtual void functionB();   //Note the keyword virtual
    void functionC();
};
 
class Child : public Parent {
public:
    void functionA();
    virtual void 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
 
    return 0;
}


[editar] Veja também