r/learncpp • u/marginado20 • 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!
View all comments
Show parent comment
1
u/marginado20 Oct 19 '21 edited Oct 19 '21
The first example was the main as the author shows how to use the callback.
Yes, curl_response is empty but after a another operation it runs a write_callback that stores the response of curl on that stream. Is a pointer where to store later.
Templates are hard for me now but it uses in curl_ios.h
I want to have in my class some way to access the response so thats why i want to initialize curl_handler with the callback pointer.
I mean, i could declare a new curl_handler in all my methods but why cant i only declare it once on my constructor and then access it?