0

Everyone,

I am writing a class in C++ which calculates points in space. I am designing this class to be independent from the application, so basically what I want -

Initialize object with input parameters -> calculate the values -> retrieve the calculated array

I prefer using C-like array (not the std::array). I'm writing the getter function -

// The definition of the ctrlpoints array
GLfloat ctrlpoints[4][4][3];

void GlCircle::getControlPoints(GLfloat* controlPoints) {
    std::copy(std::begin(ctrlpoints), std::end(ctrlpoints), std::begin(controlPoints));
}

Basically it should copy all values from ctrlpoints array to controlPoints target array. Of course compiler does not allow me to do just that (it doesn't like the type for std::begin(controlPoints)). Maybe someone can suggest an idea how should I return a copy of the array? Maybe the idea can realized in some other way, please let me know.

Thanks in advance.

4
  • 1
    "I prefer using C-like array (not the std::array)." Can you give any sensible reasoning for this? Commented Feb 15, 2015 at 18:54
  • memcpy(controlPoints, ctrlpoints, sizeof(ctrlpoints)); Commented Feb 15, 2015 at 19:00
  • I am using opengl for drawing the points and it accepts only that kind of array (as far as I know). So it would a more elegant solution, otherwise it requires type convertions.. Commented Feb 15, 2015 at 19:13
  • @rock-ass: Nope, you can obtain a pointer to the raw data of a std::array just fine. Commented Feb 15, 2015 at 19:19

1 Answer 1

3
void GlCircle::getControlPoints(GLfloat* controlPoints) {
    std::copy(std::begin(ctrlpoints), std::end(ctrlpoints), std::begin(controlPoints));
}

does not work since std::begin(ctrlpoints) and std::end(ctrlpoints) can be used to iterate over the 2D arrays of a 3D array, it doesn't automatically flatten the 3D array so you can treat it like a 1D array.

You can use three for loops to get the data from the member variable to the output argument.

void GlCircle::getControlPoints(GLfloat* controlPoints) {
    int index = 0;
    for ( int i = 0; i < 4; ++i )
    {
       for ( int j = 0; j < 4; ++j )
       {
          for ( int k = 0; k < 3; ++k )
          {
             controlPoints[index++] = ctrlpoints[i][j][k];
          }
       }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

This simple solution fits my needs and works. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.