名前空間
変種
操作

virtual function specifier

提供: cppreference.com
< cpp‎ | language

 
 
C + +言語
一般的なトピック
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.
フロー制御
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.
条件付き実行文
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.
繰り返し文
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.
文をジャンプします
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.
機能します
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
関数の宣言
ラムダ関数の宣言
関数テンプレート
の歴史。インライン指定
例外仕様 (廃止予定)
noexcept指定子 (C++11)
例外
Original:
Exceptions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
名前空間
Original:
Namespaces
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
タイプ
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)
指定子
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指定
貯蔵期間指定
constexprの指定子 (C++11)
自動指定 (C++11)
alignas指定子 (C++11)
初期化
Original:
Initialization
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
リテラル
Original:
Literals
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Original:
Expressions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
代替表現
ユーティリティ
Original:
Utilities
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
タイプ
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
型の別名宣言 (C++11)
属性 (C++11)
キャストします
Original:
Casts
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
暗黙の型変換
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
Cスタイルキャストと機能
メモリの割り当て
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.
クラス
Original:
Classes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
クラス固有の機能特性
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.
明示 (C++11)
静的
特殊なメンバ関数
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.
テンプレート
Original:
Templates
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
クラステンプレート
関数テンプレート
テンプレートの特殊化
パラメーターパック (C++11)
その他
Original:
Miscellaneous
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
インラインアセンブリ
 
関数は仮想であることを指定します
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.

目次

[編集] 構文

virtual function_declaration ;

[編集] 説明

仮想関数では、動作が派生クラスでオーバーライドできるメンバ関数です。非仮想関数とは対照的に、クラスの実際の型についてのコンパイル時の情報が含まれていない場合であっても、上書きされた動作が維持されます。これは派生クラスが基底クラスへのポインタまたは参照を使用して処理されている場合でも、オーバーライドされた仮想関数の呼び出しは、派生クラスで定義された動作を呼び出すことを意味.
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.
関数のシグネチャはオーバーライド可能にするために同じでなければなりません。
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.
オーバーライドする仮想関数の戻り値の型は必ずしもオーバーライドされた関数のものと同一である必要はありません。彼らはそれぞれ別のもの'共変であれば種類は異なってもかまいません。彼らは、次の要件を満たす場合に二つのタイプが共変のとおりです
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.
ここでは、Derived::f()としてオーバーライド関数へと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.
  • 両方のタイプのクラスへのポインタまたは参照されています。マルチレベルのポインタや参照は許可されていません.
    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.
  • Base::f()の戻り値の型のクラスは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.
  • Derived::f()の戻り値の型はCV修飾の戻り値の型より均等に以下Base::f()でなければなりません.
    Original:
    the return type of Derived::f() must be equally or less CV修飾 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.
仮想関数が直接に呼び出される場合、それは、明示的であるし、仮想呼び出しメカニズムが抑制され、その特定の実装が呼び出され、それがメンバーになっているクラスの予選.
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.
基底クラスの仮想デストラクタは、そのデストラクタはそうでなければ継承されていない場合でも、常に派生クラスのデストラクタでオーバーライドされています.
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.
仮想関数へのアクセスルールは最初の宣言によって決定されます。オーバーライドする関数の宣言で定義されたアクセスルールは、直接の関数呼び出しにのみ適用されます.
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関数指定子は、メンバーシップを意味し、このようにして唯一のメンバ関数は、仮想することができます。クラスのインスタンスは仮想関数を呼び出すために必要であるためにも、仮想関数は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.
関数テンプレートはvirtual宣言することはできません。これは、自分だけのテンプレートである関数に適用されます - クラステンプレートのレギュラーメンバー関数は、仮想宣言することができます.
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.

[編集]

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;
}


[編集] も参照してください