Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing Pointers by Reference

Status
Not open for further replies.

RichardF

Programmer
Oct 9, 2000
239
GB
Hi,

Can anybody suggest what is the best method when passing pointers to referenced parameters in methods ?

For example :

class CWidget {
public:
void Compare(CWidget& W);
};

// later ....

CWidget *Widgets;
Widgets = new CWidget[10];

// Widgets->Compare(Widgets+1); compiler error - cannot convert ...

Thanks in advance
 
Widgets->Compare( &(Widgets+1))

Not sure what your doing, but this will work as long as Widgets does not point to the last place (i.e. element 10)

Matt
 
Passing parameters by reference or by pointers internally are identical, it is only question of syntax flexibility. If you use parameters by reference, your call must be as follows:
Widgets->Compare(Widgets[1]);
If you want to pass pointers like Widgets+1, definition of your method must look like
void Compare(CWidget *W);


 
BTW.. taht & in my original post should be a *. Fat fingers strike again :p

<Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top