<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          C++核心準則ES.12:不要在嵌套作用域中重復使用同樣的名稱

          共 2339字,需瀏覽 5分鐘

           ·

          2020-04-22 23:21

          e7194a135084b265a7285c32c546df38.webp

          ES.12: Do not reuse names in nested scopes

          ES.12:?不要在嵌套作用域中重復使用同樣的名稱


          Reason(原因)

          It is easy to get confused about which variable is used. Can cause maintenance problems.

          這會導致更難弄清楚到底哪個變量在使用??赡芤鹁S護問題。


          Example, bad(反面示例)

          int d = 0;
          // ...
          if (cond) {
          // ...
          d = 9;
          // ...
          }
          else {
          // ...
          int d = 7;
          // ...
          d = value_to_be_returned;
          // ...
          }

          return d;

          If this is a large?if-statement, it is easy to overlook that a new?d?has been introduced in the inner scope. This is a known source of bugs. Sometimes such reuse of a name in an inner scope is called "shadowing".

          這一個很大的if語句,很容易漏掉內部作用域引入了一個新變量d這個事實。這是有名的錯誤源之一。這種在內部作用域中重用名稱的做法被稱為“遮蓋”。


          Note(注意)

          Shadowing is primarily a problem when functions are too large and too complex.

          “遮蓋”主要是在函數(shù)太大或者過于復雜時發(fā)生問題。


          Example(示例)

          Shadowing of function arguments in the outermost block is disallowed by the language:

          處于最外側的函數(shù)參數(shù)的遮蓋問題是被語言禁止的。


          void f(int x)
          {
          int x = 4; // error: reuse of function argument name

          if (x) {
          int x = 7; // allowed, but bad
          // ...
          }
          }
          Example, bad(反面示例)

          Reuse of a member name as a local variable can also be a problem:

          重用成員名稱作為局部變量同樣會引起問題:

          struct S {
          int m;
          void f(int x);
          };

          void S::f(int x)
          {
          m = 7; // assign to member
          if (x) {
          int m = 9;
          // ...
          m = 99; // assign to local variable
          // ...
          }
          }
          Exception(例外)

          We often reuse function names from a base class in a derived class:

          我們經常在派生類中重用基類的函數(shù)名:

          struct B {
          void f(int);
          };

          struct D : B {
          void f(double);
          using B::f;
          };

          This is error-prone. For example, had we forgotten the using declaration, a call?d.f(1)?would not have found the?int?version of?f.

          這容易引發(fā)錯誤。例如,如果我們忘記using聲明,調用d.f(1)時就無法發(fā)現(xiàn)f函數(shù)的整數(shù)版本。


          ??? Do we need a specific rule about shadowing/hiding in class hierarchies?

          我們是否需要定義一個特別的適用于類繼承情況下的遮蓋/隱藏規(guī)則?


          Enforcement(實施建議)

          • Flag reuse of a name in nested local scopes

          • 標記嵌套作用域中的名稱重用。

          • Flag reuse of a member name as a local variable in a member function

          • 標記使用成員名稱定義局部變量的情況。

          • Flag reuse of a global name as a local variable or a member name

          • 標記使用全局名稱定義局部變量和成員名稱的情況。

          • Flag reuse of a base class member name in a derived class (except for function names)

          • 標記派生類中重用基類名稱的情況(函數(shù)名稱除外)


          原文鏈接

          https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es12-do-not-reuse-names-in-nested-scopes




          覺得本文有幫助?請分享給更多人。

          關注微信公眾號【面向對象思考】輕松學習每一天!

          面向對象開發(fā),面向對象思考!


          瀏覽 65
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产成人无码Av片小说在线观看 | 精品人妻无码一区二区出白浆潮喷 | 被操的视频在线观看免费网站 | 国产一级黄片视频在线观看 | 操逼视频黄片 |