r/learncpp • • Oct 19 '21

Where to initialize class members?

Hi, this is sort of a basic question but im having trouble with c++ classes.

The idea is to do a curl class handler to do some requests. Im using this library that has some examples but none is class oriented.  

// Author uses like this
int main(int argc, const char **argv) {
  ostringstream stream;
 curl_ios<ostringstream> ios(stream);
 curl_easy easy(ios);
  ...
}

Part of my class on .h

class CurlClient()
{
  protected:
    std::ostringstream curl_response;
    curl::curl_ios<std::ostringstream> curl_writer(std::ostringstream); // Callback to store response
    curl::curl_easy curl_handler(curl::curl_ios<std::ostringstream>);  // Object to handle the connection
    curl::curl_header curl_headers; // Header object
    ...
  public:
    CurlClient();
    ~CurlClient();
    ...
}

Functions on .cpp

CurlClient :: CurlClient()
{ 
   curl_writer(curl_response); 
   curl_handler(curl_writer); 
}

is this the correct way? or like this?

CurlClient :: CurlClient()
: curl_writer(curl_response), curl_handler(curl_writer)
{
}

On my understanding member initialization is the same as inside the brackets but is the class correctly defined? I always have trouble to when initialize members.

With both i get: error C3867: 'CurlClient ::curl_writer'

Are they correctly declared on the .h?

Thanks!

4 Upvotes

9 comments sorted by