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!
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].
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:
Here's a nice, clean way:
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.To elaborate on the selected answer, observe this line
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.I've used a union like this to pass around matrices in the past:
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.
Try this. It compiles cleanly on gcc 4.1.3: