Dollarslice
Dollarslice

Reputation: 10284

pointer becomes null when constructor finishes running

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.

Upvotes: 0

Views: 1695

Answers (3)

jpalecek
jpalecek

Reputation: 47762

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.

Upvotes: 8

Alan
Alan

Reputation: 1945

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)

Upvotes: 3

Roman Ryltsov
Roman Ryltsov

Reputation: 69652

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.

Upvotes: 1

Related Questions