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!