Question

When you don't have to deal with pointers, copying/moving a struct into another one is quite easy.
In my case though, I've to deal with pointers and access rights.

protected:
          addrinfo *hints, *result;
...
hints = new addrinfo;
...
getaddrinfo(0, port.c_str(),  hints, &result); // result is protected - can't do that.

memmove/memcpy can be used, but addrinfo contains a pointer to sockaddr inside itself. I thought to use a temporary struct to pass getaddrinfo and then move everything to result, but the question is always there.
Tell me if you need some other details.

EDIT: @Mike Woolf you've given me an idea. Since the base class is an abstract one, I can even make those structs public, since the user would have no way to istantiate it. Thanks however for the answer.

Was it helpful?

Solution

In a case like this, I would write my own copying function. It could be something like

void structCopy(struct* dest, const struct* src);

Inside of this function, use memcpy() to copy everything over except for the pointers.

I had to do something like this when I was making a multiplayer game - copying pointers like this. In this kind of pattern it's typical to assume that this is a function that understands exactly what it has to do - that is, structCopy() is specifically designed for this particular struct. So it is aware that there is a sockaddr inside of the object.

So really, just hardcode inside of this function how you want to handle it. Do you want it to create a new sockaddr on the other end? Then have the copy function do that. Or whatever works.

TL;DR - Create a special copy function for your struct that knows what is inside the struct and have it handle the sockaddr according to whatever your specifications are. By making it part of the class (or a friend function) you're not breaking any rules by hardcoding the function.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top