title: C++: how to pass a variable as an argument to class constructor
body: <p>I am faced with a very puzzling issue.  I am trying to construct an object using a variable as a parameter.</p>
  
  <p>Have a look at this code please:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  class A {
  
    public:
      A()      : val(0) {};
      A(int v) : val(v) {};
  
    private:
      int val;
  };
  
  main() {
  
    int number;
  
    A a;       /* GOOD, object of type A, created without arguments */
    A b(2);    /* GOOD, object of type A, created with one argument */
    A c();     /* BAD, C++ thinks this is declaration of
                  a function returning class A as a type */
    A(d);      /* GOOD, object of type A again; no arguments */
    A(number); /* BAD, tries to re-declare "number" as object of type A */
  }
  </code></pre>
  
  <p>I think I do understand why objects "a", "b" and "d" can be created, whereas the rest can not.</p>
  
  <p>But I would really need the syntax of the last declaration, i.e.</p>
  
  <pre><code>    A(number);
  </code></pre>
  
  <p>to create a temporary object, to pass as an argument to another object.</p>
  
  <p>Any idea how to work around it?</p>
  
  <pre><code>Kind regards
  </code></pre>