Board index » delphi » Problems accessing member variables via a pointer link

Problems accessing member variables via a pointer link

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.
----------------

class TItem {
     public :
        double amount ;
        int position ;

Quote
} ;

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

Quote
} ;

void TTrolley::Purchase ( TItem *item ) {

     printf ("\n Purchased Item amount = %2.1f", item->amount );

    item = &cereal;

Quote
} ;

//-------------

main () {
     TItem *Item_ptr = new TItem;
     TTrolley Cart ;

    Cart.cereal.amount = 24.0 ;
    Cart.cereal.position = 6 ;

   Cart.Purchase(&item) ;  // expecting for *item to point to Cart.cereal
data area ....

  printf ("\n item->amount = %2.1f",  item->amount) ;   // I get garbage
value printed here ...

// Could it be that *item   is still not

// pointing to Cart.cereal data area ?

Quote
};  // end main
} ;

 

Re:Problems accessing member variables via a pointer link


Quote
JRed wrote:

> main () {

int main() {

Quote
>      TItem *Item_ptr = new TItem;
>      TTrolley Cart ;

>     Cart.cereal.amount = 24.0 ;
>     Cart.cereal.position = 6 ;

>    Cart.Purchase(&item) ;  // expecting for *item to point to Cart.cereal
> data area ....

The symbol item seems not to be declared at this point.

Please post code copied&pasted (not retyped) from what is causing you
problems. Thanks!

Re:Problems accessing member variables via a pointer link


Joe,

There are four problems with the code as shown - see below.

<snip explanation and start of code>

Quote
>void TTrolley::Purchase ( TItem *item ) {

>     printf ("\n Purchased Item amount = %2.1f", item->amount );

You haven't yet set the value of "item" or initialised the variables
in the TItem structure to which item points, so printing it out will
give garbage.

Quote
>    item = &cereal;

This merely sets the value of the pointer variable on the stack, not
the one in the calling routine (main).

Quote
>} ;

>//-------------

>main () {
>     TItem *Item_ptr = new TItem;

Using new here is unnecessary (and leaks memory!) as the pointer will
(should!) be set in the call to Cart.Purchase.

Quote
>     TTrolley Cart ;

>    Cart.cereal.amount = 24.0 ;
>    Cart.cereal.position = 6 ;

>   Cart.Purchase(&item) ;  // expecting for *item to point to Cart.cereal
>data area ....

>  printf ("\n item->amount = %2.1f",  item->amount) ;   // I get garbage
>value printed here ...

"item" is not declared here (will not compile!) - did you mean
Item_ptr?

<snip rest of code>

Here is a modified version which should work better!

class TItem {
     public :
        double amount ;
        int position ;

Quote
} ;

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

Quote
} ;

void TTrolley::Purchase ( TItem **item ) {
    *item = &cereal;
    printf ("\n Purchased Item amount = %2.1f", cereal.amount );

Quote
} ;

//-------------

main () {
    TItem *Item_ptr = NULL;
    TTrolley Cart ;

    Cart.cereal.amount = 24.0 ;
    Cart.cereal.position = 6 ;

   Cart.Purchase(&Item_ptr) ;

   printf ("\n item->amount = %2.1f",  Item_ptr->amount) ;

Quote
};  // end main

One other point: It is not good practice to make variables inside an
class "public" or to access them directly. It is better to add methods
to the class to get/set the values of internal variables and make
those variables private (or, in certain circumstances, protected).

HTH

Regards,
Andrew.
ajpaterson-compuse...@invalid.com

[To e-mail me, remove '@invalid' and replace the hyphen (-) with @. However, reply via the newsgroup, not by direct e-mail, please.]

Re:Problems accessing member variables via a pointer link


Quote
> ...It is better to add methods to the class to get/set the
> values of internal variables and make those variables
> private (or, in certain circumstances, protected).

What realistic consideration indicates that private should be preferred to
protected?

.  Ed

Re:Problems accessing member variables via a pointer link


Quote
"Ed Mulroy (TeamB)" wrote:

> What realistic consideration indicates that private should be preferred to
> protected?

That protected isn't much better than public.

Important reasons to restrict access to member variables are
- to enforce integrity constraints
- to avoid dependencies on your member variables (so that you are free to
change them when necessary)

If you grant derived classes direct access to member variables, these
advantages are gone.

Other Threads