tags:

views:

866

answers:

6

I have a class to parse a matrix that keeps the result in an array member:

class Parser
{
  ...
  double matrix_[4][4];
};

The user of this class needs to call an API function (as in, a function I have no control over, so I can't just change its interface to make things work more easily) that looks like this:

void api_func(const double matrix[4][4]);

The only way I have come up with for the caller to pass the array result to the function is by making the member public:

void myfunc()
{
  Parser parser;
  ...
  api_func(parser.matrix_);
}

Is this the only way to do things? I'm astounded by how inflexible multidimensional arrays declared like this are. I thought matrix_ would essentially be the same as a double** and I could cast (safely) between the two. As it turns out, I can't even find an unsafe way to cast between the things. Say I add an accessor to the Parser class:

void* Parser::getMatrix()
{
  return (void*)matrix_;
}

This will compile, but I can't use it, because there doesn't seem to be a way to cast back to the weirdo array type:

  // A smorgasbord of syntax errors...
  api_func((double[][])parser.getMatrix());
  api_func((double[4][4])parser.getMatrix());
  api_func((double**)parser.getMatrix()); // cast works but it's to the wrong type

The error is:

error C2440: 'type cast' : cannot convert from 'void *' to 'const double [4][4]'

...with an intriguing addendum:

There are no conversions to array types, although there are conversions to references or pointers to arrays

I can't determine how to cast to a reference or pointer to array either, albeit that it probably won't help me here.

To be sure, at this point the matter is purely academic, as the void* casts are hardly cleaner than a single class member left public!

+2  A: 

I've used a union like this to pass around matrices in the past:

union matrix {
    double dflat[16];
    double dmatr[4][4];
};

Then pass a pointer in to your setter and copy the data into the matrix in your class.

There are ways of handling this otherwise (that are more generic), but this solution tends to be the cleanest in the end, in my experience.

Cody Brocious
+15  A: 

Here's a nice, clean way:

class Parser
{
public:
   typedef double matrix[4][4];

   // ...

   const matrix& getMatrix() const
   {
      return matrix_;
   }

   // ...

private:
  matrix matrix_;
};

Now you're working with a descriptive type name rather than an array, but since it's a typedef the compiler will still allow passing it to the unchangeable API function that takes the base type.

Shog9
Beat me to it. :-)
Andrew
'cause i used a shorter type name. Think of all the time you wasted typing those extra five characters... ;-p
Shog9
And he expanded the "// ..."s as well... Nonetheless I feel I should accept the first one. Sorry Andrew!
Owen
+5  A: 

Try this. It compiles cleanly on gcc 4.1.3:

typedef double FourSquare[4][4];

class Parser
{
  private:
    double matrix_[4][4];

  public:
    Parser()
    {
        for(int i=0; i<4; i++)
          for(int j=0; j<4; j++)
            matrix_[i][j] = i*j;
    }

  public:
    const FourSquare& GetMatrix()
    {
        return matrix_;
    }
};

void api_func( const double matrix[4][4] )
{
}

int main( int argc, char** argv )
{
    Parser parser;
    api_func( parser.GetMatrix() );
    return 0;
}
Andrew
+3  A: 

I thought matrix_ would essentially be the same as a double**

In C there are true multi-dimensional arrays, not arrays of pointers to arrays, so a double[4][4] is a contiguous array of four double[4] arrays, equivalent to a double[16], not a (double*)[4].

There are no conversions to array types, although there are conversions to references or pointers to arrays Casting a value to a double[4][4] would attempt to construct one on the stack - equivalent to std::string(parser.getMatrix()) - except that the array doesn't supply a suitable constructor. You probably did't want to do that, even if you could.

Since the type encodes the stride, you need a full type (double[][] won't do). You can reinterpret cast the void* to ((double[4][4])*), and then take the reference. But it's easiest to typedef the matrix and return a reference of the correct type in the first place:

typedef double matrix_t[4][4];

class Parser
{
 double matrix_[4][4];
public:
 void* get_matrix () { return static_cast<void*>(matrix_); }

 const matrix_t& get_matrix_ref () const { return matrix_; }
};

int main ()
{
 Parser p;

 matrix_t& data1 = *reinterpret_cast<matrix_t*>(p.get_matrix());

 const matrix_t& data2 = p.get_matrix_ref();
}
Pete Kirkham
A: 

@Pete: Thanks for the explanations.

I've asked a question stemming from your wisdom.

Owen
+1  A: 

To elaborate on the selected answer, observe this line

const matrix& getMatrix() const

This is great, you don't have to worry about pointers and casting. You're returning a reference to the underlying matrix object. IMHO references are one of the best features of C++, which I miss when coding in straight C.

If you're not familiar with the difference between references and pointers in C++, read this

At any rate, you do have to be aware that if the Parser object which actually owns the underlying matrix object goes out of scope, any code which tries to access the matrix via that reference will now be referencing an out-of-scope object, and you'll crash.

Orion Edwards