Skip to main content
GameDev.net gamedev.net
🔒 Locked

C++ Pointer Question

Started by Mr Lane Mar 13, 2005 at 7:32 AM 5 replies 1.2k views
Original Post
Mr Lane
Mr Lane
Hi all Just when you think you got pointers 100% down, something new comes up. I am using a get function to get the address of variables from an class/object, the problem is the addresses arnt being returned...heres the example:

Viewport *vp_1 = NULL;
Viewport *vp_2 = NULL;
Viewport *vp_3 = NULL;
Viewport *vp_4 = NULL;

mainFrame->GetViewports(vp_1, vp_2, vp_3, vp_4);
The function GetViewports is supposed to return the addresses of the 4 viewports, which are themselves class variables (infact they are pointers to viewports). The implementation is:

void MainFrame::GetViewports(Viewport *v1,Viewport *v2,Viewport *v3,Viewport *v4)
{
    v1 = viewPort_1;    //viewPort_1 is a class variable of MainFrame here
    v2 = viewPort_2;
    v3 = viewPort_3;
    v4 = viewPort_4;
    return;
}
What that is supposed to do is copy the address of viewPort_1 (which is a pointer itself) to v1, etc. Using gdb, this works fine... The viewports are created in the MainFrame::MainFrame() constructor:

Viewport *viewport_1 = new Viewport(etc...);
Viewport *viewport_2 = new Viewport(etc...);
Viewport *viewport_3 = new Viewport(etc...);
Viewport *viewport_4 = new Viewport(etc...);
My problem is that vp_1, vp_2, vp_3, vp_4 remain NULL after the call to GetViewports (i can see in the debugger)...and I cant see why? Any ideas?
Evil Steve
Evil Steve
You need to pass a pointer to a pointer, because you want to change the value of the pointer. Just like if you want to change an int in a function, you pass a pointer to an int. When you want to change a pointer, you pass a pointer to a pointer.
This should work:
void MainFrame::GetViewports(Viewport **v1,Viewport **v2,Viewport **v3,Viewport **v4){    *v1 = viewPort_1;    //viewPort_1 is a class variable of MainFrame here    *v2 = viewPort_2;    *v3 = viewPort_3;    *v4 = viewPort_4;    return;}
snk_kid
snk_kid
Quote:
Original post by Mr Lane
void MainFrame::GetViewports(Viewport *v1,Viewport *v2,Viewport *v3,Viewport *v4){    v1 = viewPort_1;    //viewPort_1 is a class variable of MainFrame here    v2 = viewPort_2;    v3 = viewPort_3;    v4 = viewPort_4;    return;}



void MainFrame::GetViewports(Viewport*& v1,                             Viewport*& v2,                             Viewport*& v3,                             Viewport*& v4) {    v1 = viewPort_1;    v2 = viewPort_2;    v3 = viewPort_3;    v4 = viewPort_4;}


You want to modify the actual callers arguments use reference types which is the case because you want to modify the actual pointers passed in. The origional code passes the pointers by-value, a copy of the original pointers are made in general prefer reference types for real pass-by-reference.

don't use pointer to pointer, prefer a reference to a pointer.
Mr Lane
Mr Lane
Lol, it works (pointer to a pointer)...I should have posted here earlier.

As for why it works, I will come back tomorrow and read again when my headache is gone...

Thanks for the help
noVum
noVum
I would also recommend using references to pointers because this removes the double indirection headache ;)
Zahlman
Zahlman
Concur. Use the tools C++ provides you with; i.e. a reference-to-pointer in this case. It is not needlessly complex conceptually :) All you do is add the &'s to the function signature to indicate "Reflect changes to the input parameters in the calling code".
mfawcett
mfawcett
Quote:
Original post by snk_kid

...code snip...

void MainFrame::GetViewports(Viewport*& v1,                             Viewport*& v2,                             Viewport*& v3,                             Viewport*& v4) {    v1 = viewPort_1;    v2 = viewPort_2;    v3 = viewPort_3;    v4 = viewPort_4;}


You want to modify the actual callers arguments use reference types which is the case because you want to modify the actual pointers passed in. The origional code passes the pointers by-value, a copy of the original pointers are made in general prefer reference types for real pass-by-reference.

don't use pointer to pointer, prefer a reference to a pointer.


I'll chime in here on the opposite front. I prefer pointer to pointer and quite a few notable C++ resources do as well. It is not obvious to the reader that

GetViewports(v1, v2, v3, v4);

will change those variables, however, with:

GetViewports(&v1, &v2, &v3, &v4);

it is glaringly obvious. Many gurus go so far as to recommend only using const references. One thing even the second solution doesn't solve though, is ownership issues. Does the caller now own the viewports? Is he responsible for deleting them? If not, they probably should've been const pointers, or better yet, use some other mechanism (your favorite smart pointer here).
--Michael Fawcett

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.