Skip to main content
gamedev.net
🔒 Locked

pointers to pointers etc

Started by 2DamnFunky 8 replies 1k views
Original Post
2DamnFunky
2DamnFunky
hi there, ok lets say ive got a class ''thing'' like so
class thing
{
public:
    int number;
};
and i declare an object based on this class called item. ok then here we go. If i have a function that takes the address of an int as a paremeter such as void doSomething(int *x) and i wanted to pass the number from my item object id do doSomething(&item.number). If i had a pointer lpThing that pointed to my object item such as: thing *lpThing; lpThing =&item then to use it with the function id do this: doSomething(&lpThing->number); but what if i had a pointer to a pointer of type thing i.e. thing **lplpThing; *lplpThing = &item to access the number id dereference like so *lplpThing->number = 5; but how do i pass the address of the number element in my item object i.e doSomething( &(*lplpThing)->number ); this is bad syntax anyone know... i bet its something trivial liek brackets. thanks 2DamnFunky
Neosmyle
Neosmyle
No, thats the only way, really...
2DamnFunky
2DamnFunky
when i said thats bad syntax, i meant the compiler doent like it...should that work?

SoUnfaehig
SoUnfaehig
You should try to do this using some more brackets, like
doSomething( &((*lplpThing)->number) );

That should work...
2DamnFunky
2DamnFunky
got it working now. Thanks for all your help
Greatwolf
Greatwolf
There''s another option, you can change your function to use references for it''s parameters, which might make it easier syntaxwise:

void doSomething(int &x);

Then instead of having to call the function like this:

doSomething( &(*lplpThing)->number );

this will suffice:

doSomething( (*lplpThing)->number );

And the other advantage is you don''t have to keep using

*x

inside your function everytime you want to access it''s value.





--{You fight like a dairy farmer!}
--{You fight like a dairy farmer!}
2DamnFunky
2DamnFunky
yeah i did consider doing that but unfortunately the function im dealing with is not one ive creted and defined but rather one of Direct X''s.

Jedyte
Jedyte
But is the class you''re passing as an argument one you created yourself? Maybe you can make your life easier by adding a simple accessor.
And the price we paid was the price men have always paid for achieving paradise in this life -- we went soft, we lost our edge. - "Muad'Dib: Conversations" by the Princess Irulan
2DamnFunky
2DamnFunky
errrm ive only just started using classes so stuff like accessors i dont know (as of yet). I just thought a really good way to learn the ins and outs of classes would be to create a DX9 class that deals with all the various interfaces and inits.

All was going well untill i started on enumerating joystick devices. I suddenly realised that the callback function used by
IDirectInput8::EnumDevices couldnt be part of my class.

The call back function basically created and added to a linked list of DInputDevices everytime it was called and the headNode and list functions were all part of my DX9 class. Problem was i wanted to keep all these private etc and without going into the inner working of it all it meant the best way i could see of getting round this was to basically call a fucntion within the callback function that would return the address of my headNode pointer to .... drum roll..... a pointer to a pointer. Hence the question.

Oh and by the way what do you mean by ''bad coding style''... is that using accessors or something ive done?

thanks anyway for all your help.


Topic Locked

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

Sign in to reply to this topic.