C++コア準則C.128:虚関数はvirtual,overideまたはfinalと明確に定義すべきである
4229 ワード
C.128: Virtual functions should specify exactly one of virtual, override, or final
C.128:虚関数はvirtual,overideまたはfinalと明確に定義すべき
Reason
Readability. Detection of mistakes. Writing explicit virtual, override, or final is self-documenting and enables the compiler to catch mismatch of types and/or names between base and derived classes. However, writing more than one of these three is both redundant and a potential source of errors.
可読性チェックアウトエラー.virtual,overideまたはfinal自体が説明であり,コンパイラが派生クラスとベースクラスの間のタイプまたは名前の不一致問題をキャプチャできるようにする.しかしながら、この3つのうちの2つまたは3つを使用すると、余分であり、潜在的なエラーを引き起こしやすい.
It's simple and clear(次のルールは簡単で明快): virtual means exactly and only "this is a new virtual function." virualは明確に表され、「これは新しい虚関数である」 を表すためにのみ使用される. override means exactly and only "this is a non-final overrider." overideは明示的に「これは最終的なカバー者ではない」 のみを表す. final means exactly and only "this is a final overrider." finalは明確に示す、「これは最終的なカバー者である」 を表すためにのみ使用される.
Example,bad(裏面例)
Example,good(例)
Discussion(ディスカッション)
We want to eliminate two particular classes of errors:
2つの特殊なエラーを排除したいと考えています. implicit virtual: the programmer intended the function to be implicitly virtual and it is (but readers of the code can't tell); or the programmer intended the function to be implicitly virtual but it isn't (e.g., because of a subtle parameter list mismatch); or the programmer did not intend the function to be virtual but it is (because it happens to have the same signature as a virtual in the base class) 暗黙虚関数:プログラマーは関数暗黙が虚関数になることを期待し、それは本当にそうである(しかし、コードの読者には見えない).あるいはプログラマは、関数が暗黙的に虚関数になることを期待しているが、パラメータリストの小さな出入りのためではない.あるいはプログラマは関数が虚関数になることを期待していないが(ベースクラスの虚関数と同じ署名を持っているため) である. implicit override: the programmer intended the function to be implicitly an overrider and it is (but readers of the code can't tell); or the programmer intended the function to be implicitly an overrider but it isn't (e.g., because of a subtle parameter list mismatch); or the programmer did not intend the function to be an overrider but it is (because it happens to have the same signature as a virtual in the base class -- note this problem arises whether or not the function is explicitly declared virtual, because the programmer may have intended to create either a new virtual function or a new nonvirtual function) 暗黙的なオーバーライド:プログラマーは関数暗黙的にオーバーライド関数になることを期待し、それは本当にそうである(しかし、コードの読者には見えない).あるいはプログラマは、関数が暗黙的に上書き関数になることを期待しているが、パラメータリストの小さな出入りのためではない.あるいはプログラマは関数が上書き関数になることを期待していないが、それは(ベースクラスの虚関数と同じ署名を持っているため--これらの問題は、関数が虚関数として明確に宣言されているかどうかにかかわらず発生することに注意してください.プログラマが生成しようとしたのは虚関数であっても、虚関数ではない可能性があるからです)
Enforcement(実施提案) Compare virtual function names in base and derived classes and flag uses of the same name that does not override. ベースクラスと派生クラスの虚関数名を比較し、overrideではないが同じ名前を使用するよう求めた場合. Flag overrides with neither override nor final. プロンプトoverrideまたはfinaldとして宣言されていない上書き関数. Flag function declarations that use more than one of virtual, override, and final. はvirtual,override,finalの3つのキーワードを使用する2つまたは3つの関数宣言を提示する.
テキストリンク:
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c128-virtual-functions-should-specify-exactly-one-of-virtual-override-or-final
本文は役に立つと思いますか?もっと多くの人に褒めて分かち合うことを歓迎します.
更新された文章をもっと読んで、微信の公衆番号【対象に向かって考える】に注目してください.
C.128:虚関数はvirtual,overideまたはfinalと明確に定義すべき
Reason
Readability. Detection of mistakes. Writing explicit virtual, override, or final is self-documenting and enables the compiler to catch mismatch of types and/or names between base and derived classes. However, writing more than one of these three is both redundant and a potential source of errors.
可読性チェックアウトエラー.virtual,overideまたはfinal自体が説明であり,コンパイラが派生クラスとベースクラスの間のタイプまたは名前の不一致問題をキャプチャできるようにする.しかしながら、この3つのうちの2つまたは3つを使用すると、余分であり、潜在的なエラーを引き起こしやすい.
It's simple and clear(次のルールは簡単で明快):
Example,bad(裏面例)
struct B {
void f1(int);
virtual void f2(int) const;
virtual void f3(int);
// ...
};
struct D : B {
void f1(int); // bad (hope for a warning): D::f1() hides B::f1()
void f2(int) const; // bad (but conventional and valid): no explicit override
void f3(double); // bad (hope for a warning): D::f3() hides B::f3()
// ...
};
Example,good(例)
struct Better : B {
void f1(int) override; // error (caught): Better::f1() hides B::f1()
void f2(int) const override;
void f3(double) override; // error (caught): Better::f3() hides B::f3()
// ...
};
Discussion(ディスカッション)
We want to eliminate two particular classes of errors:
2つの特殊なエラーを排除したいと考えています.
Enforcement(実施提案)
テキストリンク:
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c128-virtual-functions-should-specify-exactly-one-of-virtual-override-or-final
本文は役に立つと思いますか?もっと多くの人に褒めて分かち合うことを歓迎します.
更新された文章をもっと読んで、微信の公衆番号【対象に向かって考える】に注目してください.