Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To expand on my comment…</p> <p>Based on your code, I guess you intend to let me specify a string name for each child widget I add to a <code>Container</code>, and then later to access the child widget by name. So you'd expect me to write something like this:</p> <pre><code>class LoginController { Container *container; static const char *kUsernameKey = "username"; static const char *kPasswordKey = "password"; public: LoginController() : container(new Container()) { container-&gt;addChild(kUsernameKey, new TextBox()); container-&gt;addChild(kPasswordKey, new TextBox()); container-&gt;addChild("button", new Button("Log In")); container-&gt;get&lt;Button&gt;("button")-&gt;setAction([](){ this-&gt;login(); }) } void login() { string username = container-&gt;get&lt;TextBox&gt;(kUsernameKey)-&gt;getText(); string password = container-&gt;get&lt;TextBox&gt;(kPasswordKey)-&gt;getText(); sendLoginRequest(username, password); } }; </code></pre> <p>Designing <code>Container</code> this way does lookups and type checks at run time, but those lookups and type checks could be done at compile time.</p> <p>Instead, design the API so that I keep my own, specifically-typed references to the children in my own variables. Looking up the children simply becomes using the variables, and no casting is necessary. The lookups and type checks are done at compile time. The code looks like this:</p> <pre><code>class LoginController { Container *container; TextBox *usernameBox; TextBox *passwordBox; public: LoginController() : container(new Container()), usernameBox(new TextBox()), passwordBox(new TextBox()) { container-&gt;addChild(username); container-&gt;addChild(password); Button *button = new Button("Log In"); container-&gt;addChild(button); button-&gt;setAction([](){ this-&gt;login(); }) } void login() { string username = usernameBox-&gt;getText(); string password = passwordBox-&gt;getText(); sendLoginRequest(username, password); } }; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload