0

I want to pass an IloNumArray as an argument by reference in a function like this:

void myfuntion(...., IloNumArray & X, ...)
{
  // ...
}

I know that this is not correct. Is there an other alternative?

Niall
  • 30,036
  • 10
  • 99
  • 142
user3710304
  • 27
  • 1
  • 5

1 Answers1

0

It's considered 'wrong' to pass IloXXX objects by reference because they themselves are essentially handles pointing to the actual data, so the cost of passing the IloXXX object is the same as the cost off passing a reference. Also the semantics of copying the IloXXX objects is like passing a reference, so

void myFunction(IloNumArray x) {
  x[0] = 1.0;
}

IloEnv env;
// create an array with 1 element, value of 0
IloNumArray x(env, 1, 0.0);
myFunction(x);
cout << x[0] << endl;
// 1.0
Community
  • 1
  • 1
David Nehme
  • 21,379
  • 8
  • 78
  • 117
  • This doesn't answer the question. Nothing in your answer explains why the OP's syntax wouldn't work, and from the comments on the question, it turns out it does work. It'd be useful as a comment, but not as an answer. –  Aug 18 '14 at 16:47
  • The question asks: 'is there an alternative'. Yes, there is, pass the IloNumVarArray by value. – David Nehme Aug 18 '14 at 17:05
  • By that logic, "turn off your computer" is also a valid answer... The question asks "how to pass IloNumArray as an argument by reference" -- that's an exact quote -- and asks for an alternative to the supposedly not-working syntax. –  Aug 18 '14 at 17:10
  • passing the IloNumArray by value does what he was trying to do by passing by reference. It is a true alternative. In fact it is the idiomatic way to pass them to functions. – David Nehme Aug 18 '14 at 17:25
  • Alright, that's a fair point. If it answers what the OP is actually trying to accomplish by passing it by reference, that'd make it a valid answer, and there's a fair chance it does answer that. I'm not entirely convinced it does, but I'll leave that for the OP to decide, then. –  Aug 18 '14 at 17:34