[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [ns] pointers and addresses



By looking at you code the difference between the 2 is that in the num() case you
are returning a reference to the int num_.

In the second case you are returning a pointer instead of a reference to the
pointer.

It might be easier to just create a function to set the value instead of returning
references and pointers.
For example:

hdr_rack* cr= (hdr_rack*)p->access(off_rack_);
cr->setnum(value);

Where set value looks like:

setnum(int i) {
  num_ = i;
}

- John

Jan Cerlot wrote:

> Hi,
> I have created a new header. In this header, I have a field integer num_. I can
> change its value in my main program with the two lines :
>
> hdr_rack* cr= (hdr_rack*)p->access(off_rack_);
> cr->num()=value;
>
> because, in my header, I have defined the function num() like this :
>
> int& num() {return num_;}
>
> But now, I would like to do the same with a pointer ptr_s_ to a structure.
>
> I tried this :
>
> dyn_rack *ptr_s_;
> dyn_rack* ptr_s() {return ptr_s_;}
>
> but when I try to change the value of this pointer in my main program, I have an
> error :
> non-lvalue in assignment
>
> So could anybody show me  the line to type to define the correct function in my
> header so I could change the value of the pointer ( ex.: so I could have the
> equivalency of ptr_s=malloc ... but with the function defined in my header as
> with num(), I want num=5, I write cr->num()=5 ).
>
> Thank you.
>    Jan.