(C) Theodore Norvell 1999
What is the difference between the following two subroutines:
void f( int &r ) {
r = r / 2 ;
}
void g(int *p) {
*p = *p / 2 ;
}
Think about how they are called.
Since "f"'s parameter is passed "by reference", calling it will affect the variable that is passed in.
Since "g" will change the value of the variable "p" points to.
If "i" is an int variable, we must call "f" like this: "f( i )", whereas "g" is called like this: "g( &i )".
These two calls have the same net effect.
Is there anything wrong with this subroutine?
int &odd( int &r, int i ) {
r = i*i ;
return r ; }
Since r was passed in. The "real" variable the "r" refers to, must still exist.
It returns a reference to the same variable that was passed in as its first parameter.
It is, however, not a useful for much other than illustrating this point.
Is there anything wrong with this subroutine?
int &peculiar( int &r, int i ) {
r = i*i ;
return i ; /* This line has changed */ }
"i" is a stack variable. As such it is destroyed when the subroutine is returned from. A reference to a destroyed variable is useless and dangerous (assigning to it may corrupt some innocent memory location that formerly was used to store the destroyed variable).
Note that the type of "peculiar" says it returns a reference to an int variable. Will "i" still exist when that reference is used?
Is there a difference between these two subroutines:
Student top( ClassList &cl ) {
...subroutine body...
}
Student top( const ClassList cl ) {
...subroutine body...
}
It is true they are similar, but there are some subtle differences.
What is the type of variable "x" in the following declaration.
ClassList *(x[N]) ;
Since *(x[i]) will be a ClassList object, x[i] must be a pointer to a ClassList object.
Remember: Declare them as you would use them.
By the way, the parentheses in this declaration are redundant.
Remember "Declare them as you would use them."
Assuming that "ClassList" has been defined as a type, this is a reasonable declaration.
What is the type of variable "y" declared thus:
ClassList ( *y )[N] ;
Remember: Declare them, as you'd use them.
Since "( *y )[i]" will be an object of type ClassList, "( *y )" must be an array of objects of type ClassList
Assuming that "ClassList" has been defined as a type, this is a reasonable declaration.
Which of the following pairs of declarations are NOT identical in meaning.
Modifiers on the right bind tighter than modifiers on the left.
The parentheses (and spaces) are optional.
The declaration on the right does not make sense. There is no such thing in C++ as a pointer to a reference.
Modifiers on the right bind tighter than modifiers on the left..
What is the type of "x" in the following declaration?
const char* w, x, y ;
Don't be fooled by the proximity of the * to the char. The * goes with the "w".
Don't be fooled by the proximity of the * to the char. The * goes with the "w".
The * goes with the "w" and has no effect on the type of "x". The const goes with the "char" indicating that the character will not be changed.
Note that this is a rather silly declaration, since I did not initialize "x" or "y".
The const applies to the whole declaration..
What is the name and type of this member function
Set* Set::foo(int i) const
{ ... }
It's name is "foo" and it is a member of class "Set".
The type is "function from int to pointer to Set". I.e., this member function takes an argument of type int and returns a pointer to an object of type "Set".
Moreover, calling the function does not change the recipient object (denoted by the "const").
Suppose the following declarations are made
const int *p = x ;
int *const q = y ;
For each of the following statements, is the statement sensible or not.
The const means that "p" can be used to read a variable, but not the change it.
q can be used to change int variables
"p" itself can be changed
"q" is a const pointer and so can not be changed..
This page was generated by PerlInHTML.