I have 2 class:
class Request
{
KeyValuePair* dimension;
};
class Response
{
KeyValuePair* dimension;
};
class KeyValuePair
{
public:
hash_map<string, string> hmap;
};
in one of the method, I want to populate the Response.dimension with Request.dimension but I want the Request.dimension pointing to a different address from Response.dimension?
I don’t want any change Response.dimension which also affect Request.dimension.
Is there a way to do that?
void Transformation::applyTransformations(const Request& req, Response& res)
{
res.dimension = req.dimension; // WRONG. Pointing the same address.
}
Yes, of course:
Note that if
KeyValuePairis not a POD type, you should define the assignment operator (operator =), as it will get called during the assignment. If you don’t, the compiler-generated one will be called.You should also check if
res.dimensionis properly allocated, if not, use the copy constructor: