Updating private members - unnecessary copy overhead?

Hi folks,

I have a function void update(gsl_vector * x) and as the name suggests update changes the vector x points to. I would like to use update to work on x, but x is a private variable to another class that update is not member of. x has a mutator function set_x(gsl_vector *myX).

My solution at the moment is to create a temporary pointer temp, update temp and then mutate x using the updated temp:

UGLY SOLUTION

gsl_vector * temp; // declare temp
temp = gsl_vector_alloc (n); // allocate memory for temp
update(temp); // update temp
set_x(temp); // change x

which seems to be too complicated and unnecessary copy overhead.

If update was member or friend of the class x lives in I could just use

update(x);

If I don't want to make update a member, is there a more elegant solution?

Many thanks!

Daniel


set_x(update()); with update() returning a updated object instead. I don’t think you can do anything without changing update().
Whether there is a copy overhead depends on the contents of set_x(). It doesn't have to do copying, since it is only dealing with pointers, but it probably does, since pointing to shared memory, without smart pointers can end badly.
So, unless there is such getter, which makes encapsulation pointless, there is not way to avoid copying with your update(). Is there some reason why you don't want it to be a friend or member ?
Topic archived. No new replies allowed.