• 0

[C++] Casting to an array reference


Question

Hi,

I'm having trouble understanding the proper way to cast to array reference types and I would appreciate any help you can provide!

The code I'm working on requires may small computations using coordinate pairs. I've chosen to use array references to represent (x,y) coordinate pair since they are lightweight and provide better bounds checking than do pointers. For example, here's the function header for one function that calculates the cross product of two vectors, ij and ik:

inline double cross_product(const double (&i)[2], const double (&j)[2], const double (&k)[2] )

The legacy software I'm working with generates the necessary coordinates as the result of a query, but returns them in a large 1D array:

double * data = do_query();

I know that xy coordinate pairs are always adjacent in this array, so what I want to do is create a reference to a two-element array that is an alias for two adjacent elements in a larger array. I wish I could say something like the following:

// Doesn't compile.

const double (&i)[2] = data[i_pos];

const double (&j)[2] = data[j_pos];

const double (&k)[2] = data[k_pos];

double answer = cross_product(i,j,k);

I don't see anything in the C++ standard that addresses array references specifically, and nothing short of reinterpret_cast will allow this conversion to succeed. Does anyone know whether there is a standard-compliant way of doing this?

Thanks!

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0
Hi,

I'm having trouble understanding the proper way to cast to array reference types and I would appreciate any help you can provide!

The code I'm working on requires may small computations using coordinate pairs. I've chosen to use array references to represent (x,y) coordinate pair since they are lightweight and provide better bounds checking than do pointers. For example, here's the function header for one function that calculates the cross product of two vectors, ij and ik:

inline double cross_product(const double (&i)[2], const double (&j)[2], const double (&k)[2] )

The legacy software I'm working with generates the necessary coordinates as the result of a query, but returns them in a large 1D array:

double * data = do_query();

I know that xy coordinate pairs are always adjacent in this array, so what I want to do is create a reference to a two-element array that is an alias for two adjacent elements in a larger array. I wish I could say something like the following:

// Doesn't compile.

const double (&i)[2] = data[i_pos];

const double (&j)[2] = data[j_pos];

const double (&k)[2] = data[k_pos];

double answer = cross_product(i,j,k);

I don't see anything in the C++ standard that addresses array references specifically, and nothing short of reinterpret_cast will allow this conversion to succeed. Does anyone know whether there is a standard-compliant way of doing this?

Thanks!

Why do you even have the [2] there, if you want a reference to a double it should just be double &i = data[i_pos]; instead of what you have there. The reason is because data[i_pos] is just an element in a double typed array, not a double array as in a double dimensional array. I think thats where you are getting confused.

Read this:

http://www.cprogramming.com/tutorial/references.html

Link to comment
Share on other sites

  • 0

@P_1: 'double &i = data[i_pos];' won't work for him. He basically wants to do i[1] or similar after-the fact to get the adjacent entry.

The following would work using pointers:

const double *i = &data[i_pos];
// i[0] is the first; i[1] is the second

'data[i_pos];' will always give you a double type, and hence a reference to it will always be a double.

Link to comment
Share on other sites

  • 0
@P_1: 'double &i = data[i_pos];' won't work for him. He basically wants to do i[1] or similar after-the fact to get the adjacent entry.

The following would work using pointers:

const double *i = &data[i_pos];
// i[0] is the first; i[1] is the second

'data[i_pos];' will always give you a double type, and hence a reference to it will always be a double.

I already addressed the issue of data[i_pos] always giving him a double.

double &i = data[i_pos]; will give him a reference variable to the double. This is also known as an alias. Please read the link I provided in the previous post. Also read this:

http://bytes.com/groups/c/165735-reference...iable-alias-huh

Another thing is that if he uses the method header:

inline double cross_product(const double (&i), const double (&j), const double (&k) )

he can just call cross_product(data[i_pos],data[j_pos],data[k_pos]) since it is pass by reference, he does not need to do anything with data

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.