Board index » delphi » Problems accessing member variables via pointer ..

Problems accessing member variables via pointer ..

Hi ... I have the following source code .... which I am having problems
with.
Basically, I am trying to point to an object using pointers and access its
member variables via the pointer ... no luck ...  I hope you can help shed
light on this problem ...

Cheers,
Joe.

-----------------------
#include <stdio.h>

class TItem {
   public :
      double amount ;
      int position ;

Quote
} ;

class TTrolley {
   public :
      TItem cereal ;
      void Purchase ( TItem *item) ;

Quote
} ;

void TTrolley::Purchase ( TItem *item) {
   item = &cereal ;

Quote
} ;

main () {

  TItem *item_ptr = new TItem ;
  TTrolley Cart ;

  Cart.cereal.amount = 24.0 ;  // initialise amount
  Cart.cereal.position = 6 ;   // initialise pos

  Cart.Purchase(item_ptr) ;

  //
  //   I GET GARBAGE VALUES HERE WHEN I PRINT THEM TO SCREEN ..
  //   Basically, all I want to be able to do is access the member
  //   variables of an object that a pointer is pointed to ...
  //   It is obviusly NOT working here ...
  //

  printf ("\n item_ptr->amount = %2.1f", item_ptr->amount);
  printf ("\n item_ptr->position = %d", item_ptr->position);

Quote
}

// PS: Please send a copy of your reply to jredi...@hotmail.com
//  Thank you ..
 

Re:Problems accessing member variables via pointer ..


Quote
> void TTrolley::Purchase ( TItem *item) {
>    item = &cereal ;

> } ;

Shouldn't that be
  *item = &cereal;

.  Ed

Re:Problems accessing member variables via pointer ..


Quote
JRed wrote:

> main () {

int main() {

Quote
>   Cart.Purchase(item_ptr) ;

This passes a pointer to a TItem.

Quote
> void TTrolley::Purchase ( TItem *item) {
>    item = &cereal ;
> }

The parameter item is a *copy* of item_ptr. Assigning item a different
address doesn't change item_ptr; only the copy is changed.

You basically have two choices:

1) Modify the object item_ptr and item refer to:

void TTrolley::Purchase ( TItem *item) {
   *item = cereal ;

Quote
}

2) Pass item_ptr by reference so that the assignment inside Purchase()
propagates to its caller:

void TTrolley::Purchase ( TItem * &item) {
   item = &cereal ;

Quote
}

Note that this "solution" causes the object dynamically allocated on the
first line of main() to be leaked.

Quote
> // PS: Please send a copy of your reply to jredi...@hotmail.com

No.

Re:Problems accessing member variables via pointer ..


Thanks Thomas .... it worked !!

Joe.

Quote
Thomas Maeder wrote in message <3AF6D7CB.5A107...@glue.ch>...
>JRed wrote:

>> main () {

>int main() {

>>   Cart.Purchase(item_ptr) ;

>This passes a pointer to a TItem.

>> void TTrolley::Purchase ( TItem *item) {
>>    item = &cereal ;
>> }

>The parameter item is a *copy* of item_ptr. Assigning item a different
>address doesn't change item_ptr; only the copy is changed.

>You basically have two choices:

>1) Modify the object item_ptr and item refer to:

>void TTrolley::Purchase ( TItem *item) {
>   *item = cereal ;
>}

>2) Pass item_ptr by reference so that the assignment inside Purchase()
>propagates to its caller:

>void TTrolley::Purchase ( TItem * &item) {
>   item = &cereal ;
>}

>Note that this "solution" causes the object dynamically allocated on the
>first line of main() to be leaked.

>> // PS: Please send a copy of your reply to jredi...@hotmail.com

>No.

Re:Problems accessing member variables via pointer ..


Quote
JRed wrote:

> Thanks Thomas .... it worked !!

You are welcome.

Would you please not quote entire posts you are following up to, like

Quote
> Thomas Maeder wrote in message <3AF6D7CB.5A107...@glue.ch>...
> >JRed wrote:

[deleted a lot of stuff]

Thanks!

Please have a look at the guidelines at http://www.borland.com/newsgroups

Other Threads