开发者

Passing a const double[][] array as argument to a double** interface

开发者 https://www.devze.com 2023-04-06 21:17 出处:网络
I have a 2-dim const double matrix which needs to be passed as argument to a function that takes a (non-const) double** parameter.

I have a 2-dim const double matrix which needs to be passed as argument to a function that takes a (non-const) double** parameter.

[CEqParams.h] // < given as is - can't do a thing about it

class CEqParams
{
public:
  void operator=(const CEqParams &right);  
  ...
  double dm_params[4][8];
};

.

[some_functions.h] // < dto.
...
void setEqParams(double **m);
...

.

[CEqParams.cpp]

void CEqParams::operator=(const CEqParams &right)
{
  setEqParams( « magic here » );
}

where « magic here » (in the final segment of code snippets) shall take right.dm_params (or an appropriate representation of its contents, respectively).

What else than manually transferring right.dm_params to an auxiliary double** structure (by means of a nested loop running over all array fields and copying them on开发者_StackOverflow社区e by one) and then passing the latter to setEqParams could I do here?

PS: And given I'd be able to pass right.dm_params to a function that takes a double[][] (or double[4][8]?) as parameter - how would I get rid of the const? "Hint": A const_cast<double[4][8]>(right.dm_parameters) doesn't work.


One fairly ugly way, which at least doesn't require duplicating all the data, is to create a temporary array of row pointers, e.g.

void CEqParams::operator=(const CEqParams &right)
{
  double * dm_params_ptrs[4] = { dm_params[0], dm_params[1], dm_params[2], dm_params[3] };

  setEqParams(dm_params_ptrs);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号