title: C++ data members return garbage after being defined in a separate member function
body: <p>I don't know how much of the below code is relevant to the problem, but I have a derived class with three data members (<code>loc</code>, <code>remote_host</code>, and <code>remote_port</code>). They're being declared in the class' header file, and being defined in the <code>Initialize()</code> member function. However, when the <code>HandleRequest()</code> function tries to access them, <code>remote_host</code> and <code>remote_port</code> return garbage and 0 respectively; <code>loc</code> returns <code>"/proxy"</code> as expected. Can someone point out the obvious? I'm lost here...</p>
  
  <pre><code>// in response_handler.hh
  class ResponseHandlerInterface {
    public:
      virtual bool Initialize(const NginxConfig&amp; config) = 0;
      virtual bool HandleRequest(const std::string&amp; request, std::string* response) = 0;
  };
  
  
  // in ProxyHandler.hh
  class ProxyHandler : public ResponseHandlerInterface {
    public:
      std::string loc, remote_host;
      int remote_port;
      bool Initialize(const NginxConfig&amp;);
      bool HandleRequest(const std::string&amp;, std::string*);
  };
  
  
  // in ProxyHandler.cc
  bool ProxyHandler::Initialize(const NginxConfig &amp;config) {
    loc = "/proxy";
    remote_host = "digboston.com";
    remote_port = 80;
    std::cout &lt;&lt; "Values in Initialize():" &lt;&lt; std::endl;
    std::cout &lt;&lt; loc &lt;&lt; " " &lt;&lt; remote_host &lt;&lt; " " &lt;&lt; remote_port &lt;&lt; std::endl;
    return true;
  }
  
  bool ProxyHandler::HandleRequest(const std::string &amp;request, std::string *response) {
    std::cout &lt;&lt; "Values in HandleResponse():" &lt;&lt; std::endl;
    std::cout &lt;&lt; loc &lt;&lt; " " &lt;&lt; remote_host &lt;&lt; " " &lt;&lt; remote_port &lt;&lt; std::endl;
    return true;
  }
  
  
  // in main.cc
    // a new instance of ProxyHandler is created,
    // Initialize() is called on the object,
    // HandleRequest() is called on the object.
  </code></pre>
  
  <hr>
  
  <p>Here is the output :(</p>
  
  <pre><code>&gt;&gt; ./runprogram
  
  Values in Initialize():
  /proxy digboston.com 80
  
  Values in HandleResponse():
  /proxy H?/P??P??    P??@    P??`    P?? 0
  </code></pre>
  
  <p>As you can see, <code>loc</code> retained its value. <code>remote_host</code> and <code>remote_port</code> hold their garbage values from when they were initialized. What can I do to make sure all three values are changed permanently from the <code>Initialize()</code> function??</p>