title: c++ Better way to assign a derived class to a base class
body: <p>This seems like a silly question but I want to be able to instantiate a derived class and assign it to a base class in one line:</p>
  
  <pre><code>class A { };
  class B : public A { };
  
  // i can do this
  A *base;
  B derived;
  base = &amp;derived;
  
  // i'd like to do something like this
  A *base;
  base = &amp;B(); // produces a warning: taking address of temporary
  </code></pre>
  
  <p>I'm trying to get polymorphic behavior without using the new operator. I think the compiler is inserting a temporary reference to the return value of the constructor. Is it possible to instruct the compiler that I actually want to use that address for the assignment? </p>
  
  <p><em>*</em> EDIT</p>
  
  <p>Here's a little context. I have something like this when parsing an xml file:</p>
  
  <pre><code>class element { virtual void load_xml(...); };
  class city : public element { ... };
  class state : public element { ... };
  
  element *base;
  // read some xml
  
  if (xml_node == "city") {
    base = &amp;city(); 
  } else if (xml_node == "state") {
    base = &amp;state();
  } 
  
  base.load_xml(...);
  </code></pre>
  
  <p>The idea is that each derived node knows how to hydrate itself and will override the load_xml() function to do so. I guess what I should really be doing is calling load_xml in each if block rather than try and assign each derived class to the base then call load_xml from the base at the end.</p>
  
  <p>Thanks!</p>