To ensure `only scope can create a variable`, we should mark `Variable`'s constructor as a private member function, and Scope is a friend class of Variable. And then only `NewVar` can construct `Variable`.
To ensure `only scope can create a variable`, we should mark `Variable`'s constructor as a private member function, and Scope is a friend class of Variable. And then only `Var` can construct `Variable`.
## When scope destroyed, all variables inside this scope should be destroyed together
## When scope destroyed, all variables inside this scope should be destroyed together
...
@@ -121,4 +121,4 @@ Also, as the parent scope is a `shared_ptr`, we can only `Create()` a scope shar
...
@@ -121,4 +121,4 @@ Also, as the parent scope is a `shared_ptr`, we can only `Create()` a scope shar
## Orthogonal interface
## Orthogonal interface
`FindVar` will return `nullptr` when `name` is not found. It can be used as `Contains` method. `NewVar` will return an `Error` when there is a name conflict locally. Combine `FindVar` and `NewVar`, we can implement `NewVar` easily.
`FindVar` will return `nullptr` when `name` is not found. It can be used as `Contains` method. `Var` will return an `Error` when there is a name conflict locally. Combine `FindVar` and `Var`, we can implement `Var` easily.
<spanid="only-scope-can-create-a-variable"></span><h2>Only scope can create a variable<aclass="headerlink"href="#only-scope-can-create-a-variable"title="Permalink to this headline">¶</a></h2>
<spanid="only-scope-can-create-a-variable"></span><h2>Only scope can create a variable<aclass="headerlink"href="#only-scope-can-create-a-variable"title="Permalink to this headline">¶</a></h2>
<p>To ensure <codeclass="docutils literal"><spanclass="pre">only</span><spanclass="pre">scope</span><spanclass="pre">can</span><spanclass="pre">create</span><spanclass="pre">a</span><spanclass="pre">variable</span></code>, we should mark <codeclass="docutils literal"><spanclass="pre">Variable</span></code>‘s constructor as a private member function, and Scope is a friend class of Variable. And then only <codeclass="docutils literal"><spanclass="pre">NewVar</span></code> can construct <codeclass="docutils literal"><spanclass="pre">Variable</span></code>.</p>
<p>To ensure <codeclass="docutils literal"><spanclass="pre">only</span><spanclass="pre">scope</span><spanclass="pre">can</span><spanclass="pre">create</span><spanclass="pre">a</span><spanclass="pre">variable</span></code>, we should mark <codeclass="docutils literal"><spanclass="pre">Variable</span></code>‘s constructor as a private member function, and Scope is a friend class of Variable. And then only <codeclass="docutils literal"><spanclass="pre">Var</span></code> can construct <codeclass="docutils literal"><spanclass="pre">Variable</span></code>.</p>
<spanid="when-scope-destroyed-all-variables-inside-this-scope-should-be-destroyed-together"></span><h2>When scope destroyed, all variables inside this scope should be destroyed together<aclass="headerlink"href="#when-scope-destroyed-all-variables-inside-this-scope-should-be-destroyed-together"title="Permalink to this headline">¶</a></h2>
<spanid="when-scope-destroyed-all-variables-inside-this-scope-should-be-destroyed-together"></span><h2>When scope destroyed, all variables inside this scope should be destroyed together<aclass="headerlink"href="#when-scope-destroyed-all-variables-inside-this-scope-should-be-destroyed-together"title="Permalink to this headline">¶</a></h2>
...
@@ -295,7 +295,7 @@
...
@@ -295,7 +295,7 @@
</div>
</div>
<divclass="section"id="orthogonal-interface">
<divclass="section"id="orthogonal-interface">
<spanid="orthogonal-interface"></span><h2>Orthogonal interface<aclass="headerlink"href="#orthogonal-interface"title="Permalink to this headline">¶</a></h2>
<spanid="orthogonal-interface"></span><h2>Orthogonal interface<aclass="headerlink"href="#orthogonal-interface"title="Permalink to this headline">¶</a></h2>
<p><codeclass="docutils literal"><spanclass="pre">FindVar</span></code> will return <codeclass="docutils literal"><spanclass="pre">nullptr</span></code> when <codeclass="docutils literal"><spanclass="pre">name</span></code> is not found. It can be used as <codeclass="docutils literal"><spanclass="pre">Contains</span></code> method. <codeclass="docutils literal"><spanclass="pre">NewVar</span></code> will return an <codeclass="docutils literal"><spanclass="pre">Error</span></code> when there is a name conflict locally. Combine <codeclass="docutils literal"><spanclass="pre">FindVar</span></code> and <codeclass="docutils literal"><spanclass="pre">NewVar</span></code>, we can implement <codeclass="docutils literal"><spanclass="pre">NewVar</span></code> easily.</p>
<p><codeclass="docutils literal"><spanclass="pre">FindVar</span></code> will return <codeclass="docutils literal"><spanclass="pre">nullptr</span></code> when <codeclass="docutils literal"><spanclass="pre">name</span></code> is not found. It can be used as <codeclass="docutils literal"><spanclass="pre">Contains</span></code> method. <codeclass="docutils literal"><spanclass="pre">Var</span></code> will return an <codeclass="docutils literal"><spanclass="pre">Error</span></code> when there is a name conflict locally. Combine <codeclass="docutils literal"><spanclass="pre">FindVar</span></code> and <codeclass="docutils literal"><spanclass="pre">Var</span></code>, we can implement <codeclass="docutils literal"><spanclass="pre">Var</span></code> easily.</p>
To ensure `only scope can create a variable`, we should mark `Variable`'s constructor as a private member function, and Scope is a friend class of Variable. And then only `NewVar` can construct `Variable`.
To ensure `only scope can create a variable`, we should mark `Variable`'s constructor as a private member function, and Scope is a friend class of Variable. And then only `Var` can construct `Variable`.
## When scope destroyed, all variables inside this scope should be destroyed together
## When scope destroyed, all variables inside this scope should be destroyed together
...
@@ -121,4 +121,4 @@ Also, as the parent scope is a `shared_ptr`, we can only `Create()` a scope shar
...
@@ -121,4 +121,4 @@ Also, as the parent scope is a `shared_ptr`, we can only `Create()` a scope shar
## Orthogonal interface
## Orthogonal interface
`FindVar` will return `nullptr` when `name` is not found. It can be used as `Contains` method. `NewVar` will return an `Error` when there is a name conflict locally. Combine `FindVar` and `NewVar`, we can implement `NewVar` easily.
`FindVar` will return `nullptr` when `name` is not found. It can be used as `Contains` method. `Var` will return an `Error` when there is a name conflict locally. Combine `FindVar` and `Var`, we can implement `Var` easily.
<spanid="only-scope-can-create-a-variable"></span><h2>Only scope can create a variable<aclass="headerlink"href="#only-scope-can-create-a-variable"title="永久链接至标题">¶</a></h2>
<spanid="only-scope-can-create-a-variable"></span><h2>Only scope can create a variable<aclass="headerlink"href="#only-scope-can-create-a-variable"title="永久链接至标题">¶</a></h2>
<p>To ensure <codeclass="docutils literal"><spanclass="pre">only</span><spanclass="pre">scope</span><spanclass="pre">can</span><spanclass="pre">create</span><spanclass="pre">a</span><spanclass="pre">variable</span></code>, we should mark <codeclass="docutils literal"><spanclass="pre">Variable</span></code>‘s constructor as a private member function, and Scope is a friend class of Variable. And then only <codeclass="docutils literal"><spanclass="pre">NewVar</span></code> can construct <codeclass="docutils literal"><spanclass="pre">Variable</span></code>.</p>
<p>To ensure <codeclass="docutils literal"><spanclass="pre">only</span><spanclass="pre">scope</span><spanclass="pre">can</span><spanclass="pre">create</span><spanclass="pre">a</span><spanclass="pre">variable</span></code>, we should mark <codeclass="docutils literal"><spanclass="pre">Variable</span></code>‘s constructor as a private member function, and Scope is a friend class of Variable. And then only <codeclass="docutils literal"><spanclass="pre">Var</span></code> can construct <codeclass="docutils literal"><spanclass="pre">Variable</span></code>.</p>
<spanid="when-scope-destroyed-all-variables-inside-this-scope-should-be-destroyed-together"></span><h2>When scope destroyed, all variables inside this scope should be destroyed together<aclass="headerlink"href="#when-scope-destroyed-all-variables-inside-this-scope-should-be-destroyed-together"title="永久链接至标题">¶</a></h2>
<spanid="when-scope-destroyed-all-variables-inside-this-scope-should-be-destroyed-together"></span><h2>When scope destroyed, all variables inside this scope should be destroyed together<aclass="headerlink"href="#when-scope-destroyed-all-variables-inside-this-scope-should-be-destroyed-together"title="永久链接至标题">¶</a></h2>
<p><codeclass="docutils literal"><spanclass="pre">FindVar</span></code> will return <codeclass="docutils literal"><spanclass="pre">nullptr</span></code> when <codeclass="docutils literal"><spanclass="pre">name</span></code> is not found. It can be used as <codeclass="docutils literal"><spanclass="pre">Contains</span></code> method. <codeclass="docutils literal"><spanclass="pre">NewVar</span></code> will return an <codeclass="docutils literal"><spanclass="pre">Error</span></code> when there is a name conflict locally. Combine <codeclass="docutils literal"><spanclass="pre">FindVar</span></code> and <codeclass="docutils literal"><spanclass="pre">NewVar</span></code>, we can implement <codeclass="docutils literal"><spanclass="pre">NewVar</span></code> easily.</p>
<p><codeclass="docutils literal"><spanclass="pre">FindVar</span></code> will return <codeclass="docutils literal"><spanclass="pre">nullptr</span></code> when <codeclass="docutils literal"><spanclass="pre">name</span></code> is not found. It can be used as <codeclass="docutils literal"><spanclass="pre">Contains</span></code> method. <codeclass="docutils literal"><spanclass="pre">Var</span></code> will return an <codeclass="docutils literal"><spanclass="pre">Error</span></code> when there is a name conflict locally. Combine <codeclass="docutils literal"><spanclass="pre">FindVar</span></code> and <codeclass="docutils literal"><spanclass="pre">Var</span></code>, we can implement <codeclass="docutils literal"><spanclass="pre">Var</span></code> easily.</p>