0

I have a camera class that has two pointers as parameters:

Camera::Camera(
    float fOVDeg, float nearCull, float farCull,
    float xPos, float yPos, float zPos,
    D3DMATRIX* matProjection, D3DMATRIX* matView)
{
    this->SetCamera(fOVDeg, nearCull, farCull);
    this->AdjustCamera(xPos, yPos, zPos);

    matProjection = &(this->projection);//gets the addresses of two private members
    matView = &(this->view);
}

and this is the code that calls it:

D3DMATRIX* matProjection,* matView;

//called once
void Initialise(HWND hWnd)
{
    camera = new Camera(
            45.0f, 1.0f, 100.0f,
            0.0f, 9.0f, 24.0f,
            matProjection, matView);

...rest of code...

basically the problem is that I want the two pointers in the code that calls the camera constructor to retain the memory addresses of the camera classes two private members so I can do stuff with 'em! trouble is, this works fine, but as soon as the constructor is finished the pointers become null (0x00000000). I have no idea why! The camera's private members still have values because I was just grabbing their values with getters before and it worked fine.

iammilind
  • 68,093
  • 33
  • 169
  • 336
Dollarslice
  • 9,917
  • 22
  • 59
  • 87

3 Answers3

8

You are passing matProjection by value, therefore, the assignments to it in the constructor assign to a local copy (which is not visible outside). Try using a reference:

Camera::Camera(
    float fOVDeg, float nearCull, float farCull,
    float xPos, float yPos, float zPos,
    D3DMATRIX* &matProjection, D3DMATRIX* &matView)
---------------^        ------------------^

The rest of the code should stay the same.

Responses to your comments:

one more thing I don't quite understand is why I don't have to place an '&' in front of matProjection and matView in the call

Because when the function parameter is declared as a reference, the compiler figures that out and passes a reference (probably implemented as a pointer) instead.

am I right in thinking that you pass the value it points to.. therefore you need to reference is to get the address of the actual pointer?

Yes. Pointers are no different than ints, and under normal conditions, their value is passed. When you need the function to set your variable (of pointer type, but the same applies to ints), you have to either pass a pointer-to-pointer or a reference-to-pointer, which are similar, but the latter comes with neat syntactic sugar.

jpalecek
  • 47,058
  • 7
  • 102
  • 144
  • As far as I know there is no need to pass pointers as references. – Tom Knapen Sep 30 '11 at 12:09
  • @TomKnapen: Apparently, the OP perceives that need when he wants to fiddle with the classes' internals... – jpalecek Sep 30 '11 at 12:18
  • CRAZY! That really cleared up some false ideas I had about pointers. Of course you're right - usually just passing the value of a pointer that's already set around will suffice, but in this case you need the memory address of the pointer, not the value it's pointing to. You just blew my tiny mind! thank you. – Dollarslice Sep 30 '11 at 13:10
  • one more thing I don't quite understand is why I don't have to place an '&' in front of matProjection and matView in the call. what is actually getting passed through when you pass a pointer? the memory address of the pointer or the memory address that it points to? – Dollarslice Sep 30 '11 at 13:22
  • am I right in thinking that you pass the value it points to.. therefore you need to reference is to get the address of the actual pointer? sorry for all the comments. – Dollarslice Sep 30 '11 at 13:23
3

You are passing the pointers in by value. So the Camera constructor is assigning values to copies of those pointers. You need to pass the pointers in by reference...

Camera::Camera(
    float fOVDeg, float nearCull, float farCull,
    float xPos, float yPos, float zPos,
    D3DMATRIX*& matProjection, D3DMATRIX*& matView)
Alan
  • 1,895
  • 1
  • 10
  • 9
1

See another today's question: Why is my pointer not null after free?

You need a reference type argument, or a pointer to pointer. By-value arguments won't do your task.

Community
  • 1
  • 1
Roman R.
  • 68,205
  • 6
  • 94
  • 158