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

Recommended Answers

All 2 Replies

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).

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.