0

I promice everone that I have looked and looked and looked for a solution, I'm not just having a problem and being lazy putting it on daniweb... haha :) ull have to trust me, ive even googled it :O!

Ok, so I am wondering, if I wanted to return a single (non aray) pointer to an array (yes, I know, pointers arn't arrays, I mean, doesn't return the start of the array) which is un modifiable, will this work:

GLdouble rotMatrix[2][2];
const GLdouble * getRotMatrix(int row, int column){return(&rotMatrix[row][column]);};

I know I can just test it... But I'm worried that I will mis-understand somthing going on and leave an error in there :\ If that isn't correct, how could I return a pointer to a single GLdouble (think of it as a double) so that a user my use the returned value to multiply another matrix and NOT be able to modify the returned value

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by falconmick
0

There is no technical issue with your code.

But you have some other small issues. First, if you don't intend to return a writable variable, then you should make that member function const (if it is indeed a member function, which is what I assume). Second, returning a pointer is in general not recommended given the fact that a pointer is easily copyable, a reference is preferred. Finally, there is no real point in outputting a reference to such a small type as a double variable, prefer just outputting it by value instead. That would lead to this:

//Either returning a const reference:
const GLdouble& getRotMatrix(int row, int column) const { return rotMatrix[row][column]; };

//Or returning by value:
GLdouble getRotMatrix(int row, int column) const { return rotMatrix[row][column]; };

Both of the above function are virtually the same.

But if you absolutely need a pointer (because you need a pointer for some OpenGL function), then your code is OK (except that you should make the member function const).

Edited by mike_2000_17: n/a

0

Thanks, I more alas wanted to know how to pass an object my constant reference, as Ive had times where Ive been confident with passing 8mb objects by constant reference, so I thought Ide try to practice :P

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.