how to initialize an object in c++ conditionally
<pre><code>class X {
   X(int, int); //constructor
   void func(); 
}

class Y {
  public int func() {
    X x(5,7); 
    // some other random operations. 
    x.func(); 
  }
}
</code></pre>

<p>Now, if I have to just initialize x based on some conditional, how do I do it? </p>

<pre><code>class Z {
 // only refer to x when mycond is true. 
  public int func(boolean mycond) {
    if(mycond) 
     X x(5,7); 
    //same random operations; 
    if(mycond) 
      x.func(); 
  }
}
</code></pre>

<p>One way to do the above in an unaesthetic manner is: </p>

<pre><code>class Z {
  // only refer to x when mycond is true. 
  public int func(boolean mycond) {
    if(mycond)  {
       X x(5,7); 
     //same random operations; 
      x.func(); 
    }
    else  {
      //same random operations
    }
  }
}
</code></pre>

<p>I am looking for something more simpler where I don't have to repeat the code. </p>
