What is the best practice for passing structures as an output in C++11?
Should the struct be created in the outer function or the inner when the ownership is to be maintained by the outer function?
And, should it be a shared_ptr or unique_ptr?
For example, suppose I have a complex struct along the lines of:
struct Error {
string Code;
string Message;
string Details;
string Command;
};
struct Response {
stringstream Data;
bool Success;
Error Error;
};
Then, from the calling function, I want to state:
Response r;
getResponse( url, &r );
Should getResponse be defined as:
getResponse( string, shared_ptr ); or
getResponse( string, unique_ptr );
Also, how should the strings, and stringstreams be declared? Should they also be shared_ptr?
In this case, the getResponse function does not want anything to do with the response once it is done, and the lifetime of the response itself should be up to the caller.
What is the best practice for declaring and populating the struct?
Aucun commentaire:
Enregistrer un commentaire