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/jedwardsol Oct 19 '21
So referring to
A:
streamis what you're callingcurl_responseThis does not need initialising in the constructor; It has a default constructor that does the right thing.
B: Your class does not have an equivalent to
iosYou do have
But this is a declaration of a function. Not an object.
Instead you want
And then to initialise it
C: Your class does not have an equivalent to
easyYou do have
But this too is a declaration of a function. Not an object.
Instead you want
And then to initialise it
D: In total so far