0

How can I convert from a double ** to a const double**

I am using ROOT and with a deconvolution function. When running, I receive an error:

cannot initialize a parameter of type 'const Double_t **' (aka 'const double **') with an lvalue of type 'Double_t **'(aka 'double **')

I define the parameter as such:

Double_t ** resp = new Double_t * [ybins];
for (int f = 0; f < xbins ; f = f+1){ 
    for (int i = 0; i <  ybins; i = i+1){
            resp[f][i] = hinr->GetBinContent(i+1,f+1);
           } 
     }
ctzdev
  • 646
  • 2
  • 9
  • 24
Daniel Shy
  • 3
  • 1
  • 4
  • 1
    Your code is broken anyway: you create a dynamic array `resp` of `ybins` `double*` which are not initialised (`resp[f]` can be anything, but certainly does it not point to an array of `double`s); then you assign to `resp[f][i]`. – Walter Jul 24 '15 at 17:17
  • 1
    Show us the real code. There is no `const` in the code snippet you have given us, so how did you get that error? – Aaron McDaid Jul 24 '15 at 17:23

1 Answers1

0

Note: the answer to the "duplicate" question explains why the implicit cast does not work but it doesn't answer this question, "How do I do what I need to do?" That answer is:

Use either a c style cast or a c++ style const_cast. The c++ const_cast is preferred because it makes your intent more clear.

#include <iostream>

int main(int argc, char * argv[])
{
  double a = 3.14159;
  double * b = & a;
  double **c = & b;

  const double ** d = (const double **) c;
  std::cout <<  **d << std::endl;
  // const_cast is the preferred approach.    
  const double ** e = const_cast<const double **> (c);
  std::cout <<  **e << std::endl;


  return 0;
}
Dale Wilson
  • 9,166
  • 3
  • 34
  • 52