passing pointers to methods

Hi all.

I have a linked list of items, and a class which stores a pointer to one item of the list (the focused element).

a method returns this pointer:

Item* returnfocusp()

suppose I have a
Item* new_focus
which is a pointer to an item that should be the new focus.

the code could look something like:

Item * dummy;
dummy= returnfocusp();
changefocus(dummy,new_focus); //to be specified

would not work, as it would at its best change the pointer dummy.

I guess it would need a
Item ** returnfocuspp() routine.

One could then
Item** dummy=returnfocuspp();

changefocus(dummy,new_focus){
*dummy=new_focus;
}

or not?

if this works at all, it smells like c and not c++.
is there a best c++ practice for such use cases?

tanks

alex


Last edited on

Guess I could use a set method for the focus which takes a pointer.

for situations where I don't want to call that method directly (e.g. when browsing the list, on success, a pointer to the new Item should be returned), one could use Item * &:

browse(Item * startItem, Item*& foundItem);

could be called as follows:
Item* dummy

browse(startitem,dummy);

one could then call setfocus(dummy) afterwards.

Would this be good practice?


Topic archived. No new replies allowed.