Hey.. ya.. I still find safe conversion between references and pointers vague. I've been meaning to read more, but I haven't had any problems, and I can't always tell if the code is flawed. This is the kind of fundamental knowledge I know there will be comments. ;)
Let's take for example a custom struct used for the cURL write callback (4th parameter - userp).
In this case inside write_data should I just convert userp to a CData pointer and use it as a pointer instead of dereferencing it? Do I need another -if- check on the pointer after converting to CData and before dereferencing it? Also should I instead create cdata dynamically instead, when passing it to curl_easy_setopt (instead of passing the reference)? I guess it would depend if the object goes out of scope and is destroyed? Opinions please and thanks again!
Code:struct CData
{
std::string Host;
unsigned int Port;
};
size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp)
{
if(userp)
{
CData& cdata = *(CData*)userp;
std::cout << cdata.Host << ":" << cdata.Port << std::endl;
}
return nmemb;
}
//later in main()
CData cdata;
cdata.Host = "google.com";
cdata.Port = 80;
//later in main()
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &cdata);