This is a discussion on if $a =& $b is assignment by reference, why don't you need to dereference it? within the PHP Language forums, part of the PHP Programming Forums category; Summercool wrote: > First of all, I think in the very traditional and basic form of > "reference", ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Summercool wrote:
> First of all, I think in the very traditional and basic form of > "reference", it means "pointers". > > So that's why in C, > > when you say > > int *ip; > > i is a pointer or "reference" to an integer. and that's why when you > use it to get back the integer, you need to do *ip and that's called > "dereference". > No. C does not have references. ip is a pointer. Nothing more, nothing less. > So in C++, it seems that there is a different kind of reference, and > that's like an alias type of reference? So in C++, Java, and PHP, you > can have > > int i = 10 > int &j = i > printf "%d", j and you get 10? > j = 20 > printf "%d %d", i, j and both are 20 now? > j is a reference to i. > that's different from the traditional pointer reference > > a = 10; b = 20 > int *ip = &a // ip pointers to an integer > int *jp = ip // jp pointers to the same integer > printf "%d", *jp > *jp = 20 > printf "%d %d", *ip, *jp > > Pointers ARE NOT REFERENCES! -- ================== Remove the "x" from my email address Jerry Stuckle JDS Computer Training Corp. jstucklex@attglobal.net ================== |
|
|||
|
Summercool wrote:
> sorry, the message was accidentally posted before it was complete: > > First of all, I think in the very traditional and basic form of > "reference", it means "pointers". > > So that's why in C, > > when you say > > int *ip; > > ip is a pointer or "reference" to an integer. and that's why when you > use it to get back the integer, you need to do *ip and that's called > "dereference". > > So in C++, it seems that there is a different kind of reference, and > that's like an alias type of reference? So in C++, Java, and PHP, you > can have > > a = 10; b = 20; > int i = a; > int &j = i; > printf "%d", j; and you get 10? > j = b; > printf "%d %d", i, j; and both are 20 now? > > that's different from the traditional pointer reference > > a = 10; b = 20; > int *ip = &a; // ip pointers to an integer > int *jp = ip; // jp pointers to the same integer > printf "%d", *jp; // print 10 > jp = &b; // now jp pointer to a different integer > printf "%d %d", *ip, *jp; // now it prints 10 and 20 > > > the first behavior is the same as PHP's > $a =& $b > That is correct. $a is a reference to $b. > the second behavior is the same as PHP5's object assignment: > $obj1 = $obj2 > In PHP5, objects are automatically references. If you want a new copy, you need a clone() method. > so it seems like both are called a reference? > Yes, the only difference being the first is explicit, the second implicit. > Isn't there standard names for their difference? The first one is > more like "an alias reference". The second one is like "a pointer > reference". I think calling them the same as "reference" is very > dangerous as they mean different things and behave differently. > They are both just references. > > some discussions: > http://en.wikipedia.org/wiki/C%2B%2B_reference > http://en.wikipedia.org/wiki/Referen...ter_science%29 > > > I haven't looked at them, but don't believe everything you see in wikipedia. -- ================== Remove the "x" from my email address Jerry Stuckle JDS Computer Training Corp. jstucklex@attglobal.net ================== |