| after |
after |
after |
|
![]() |
![]() |
![]() |
![]() |
The three functions f( ), g( ), h( ) demonstrate those three respective styles of parameter passing.
As the parameter is passed by value, f() has the effect of incrementing the formal parameter x before it is outputted. However, the actual parameter of the caller will not be effected. The effect can be observed if we execute the following code
int j(5);
f(j); // outputs 6
cout << j; // outputs 5 (unchanged by f)
As the parameter is passed by reference, g() has the effect of incrementing the formal parameter, and thus indirectly the actual parameter as well. We observe this effect as
int k(5);
g(k); // outputs 6
cout << k; // outputs 6 (changed by g)
Function h() is illegal, and would be rejected by the compiler. It is similar to g(), however since the parameter is passed by constant reference, the body of the function is not allowed to alter the parameter.
To best understand the initial block of code, we walk through the commands one at a time, drawing a sample diagram for each stage.
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
Note well that when the copy constructor was used for c as well as for f, a new instance was created each time; b was simply used as the model for the state, at that time.
Based upon the final configuration shown above, the output of the six statements is as follows.
Cat In the Hat
Snow
Snow
Snow
Cat In the Hat
Snow
The command
changes the state of that object. Note well, however, that this has no effect on c or f which were truly independent objects from b. The resulting picture is portrayed asb.setBook("Hop on Pop");
Cat In the Hat
Hop on Pop
Snow
Snow
Cat In the Hat
Snow
The command
changes the state of that object. Note well that d is truly a pointer to the same underlying object, and therefore this implicitly changes *d. The resulting picture is portrayed asc.setBook("Hop on Pop");
Cat In the Hat
Snow
Hop on Pop
Hop on Pop
Cat In the Hat
Snow
The command
assigns the state of object a to be equal to the current state of b. The resulting picture is portrayed asa = b;
Snow
Snow
Snow
Snow
Cat In the Hat
Snow
The command
assigns the state of object b to be equal to the current state of a. The resulting picture is portrayed asb = a;
Cat In the Hat
Cat In the Hat
Snow
Snow
Cat In the Hat
Snow
The commands
first assigns the state of b equal to the current state of a. The second command changes the state of a (yet has no effect on b). The resulting picture is portrayed asb=a; a.setBook("Hop on Pop");
Hop on Pop
Cat in the Hat
Snow
Snow
Cat In the Hat
Snow
The first command
assigns the value of pointer e to be equal to the current value of pointer d (i.e., 239). After this, the subsequent commande=d;
is changing the state of the object at address 239. Therefore, this has the apparent effect of changing c, *d and *e, all of which refer to the same object. The resulting picture is portrayed asd->setBook("Hop on Pop");
Cat In the Hat
Snow
Hop on Pop
Hop on Pop
Hop on Pop
Snow
The first command
assigns the value of the object pointed to by e (i.e., that at address 246) to be equal to the object pointed to by d. This first change is made moot by the subsequent*e=*d;
changes the object at address 239. The resulting picture is portrayed asd->setBook("Hop on Pop");
Cat In the Hat
Snow
Hop on Pop
Hop on Pop
Snow
Snow