References and Declarations.

UP

(C) Theodore Norvell 1999

Question 0

What is the difference between the following two subroutines:

void f( int &r ) {
    r = r / 2 ;
}

void g(int *p) {
   *p = *p / 2 ;
}

  1. Only the name.
  2. "f" has no net effect. "g" assigns an integer the floor of half its original value.
  3. "g" has no net effect. "f" assigns an integer the floor of half its original value.
  4. Calling "g" requires an address to be passed in.

No

Think about how they are called.

No

Since "f"'s parameter is passed "by reference", calling it will affect the variable that is passed in.

No

Since "g" will change the value of the variable "p" points to.

Yes

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.


Question 1

Is there anything wrong with this subroutine?

int &odd( int &r, int i ) {
   r = i*i ;
   return r ; }

  1. Yes. It may return a reference to a location that no longer exists.
  2. No. There is nothing wrong with it.

No

Since r was passed in. The "real" variable the "r" refers to, must still exist.

Yes.

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.


Question 2

Is there anything wrong with this subroutine?

int &peculiar( int &r, int i ) {
   r = i*i ;
   return i ; /* This line has changed */ }

  1. Yes. It returns a reference to a variable that will no longer exist.
  2. No. It is fine.

Yes

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

No

Note that the type of "peculiar" says it returns a reference to an int variable. Will "i" still exist when that reference is used?


Question 3

Is there a difference between these two subroutines:

Student top( ClassList &cl ) {
    ...subroutine body...
}

Student top( const ClassList cl ) {
    ...subroutine body...
}

  1. No. They are interchangeable.
  2. Yes. They are similar, but there are some differences

No

It is true they are similar, but there are some subtle differences.

Yes

 


Question 4

What is the type of variable "x" in the following declaration.

ClassList *(x[N]) ;

  1. "x" is an array of pointers to objects of type ClassList
  2. "x" is a pointer to an array of ClassList objects
  3. The declaration is ill-formed and will cause a compile time error.

Yes

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.

No

Remember "Declare them as you would use them."

No

Assuming that "ClassList" has been defined as a type, this is a reasonable declaration.


Question 5

What is the type of variable "y" declared thus:

ClassList ( *y )[N] ;

  1. "y" is an array of pointers to objects of type ClassList.
  2. "y" is a pointer to an array of objects of type ClassList.
  3. The declaration is ill-formed and will cause a compile time error.

No

Remember: Declare them, as you'd use them.

Yes

Since "( *y )[i]" will be an object of type ClassList, "( *y )" must be an array of objects of type ClassList

No

Assuming that "ClassList" has been defined as a type, this is a reasonable declaration.


Question 6

Which of the following pairs of declarations are NOT identical in meaning.

  1. int *a[10];      int *(a[10]) ;
  2. int *( *q );        int **q ;
  3. int *&a = b;       int &*a = b;  
  4. int *f() ;       int *( f() ) ;

No

Modifiers on the right bind tighter than modifiers on the left.

No

The parentheses (and spaces) are optional.

Yes

The declaration on the right does not make sense.  There is no such thing in C++ as a pointer to a reference.

No

Modifiers on the right bind tighter than modifiers on the left..


Question 7

What is the type of "x" in the following declaration?

const char* w, x, y ;

  1. "x" is a constant pointer to a character
  2. "x" is a pointer to a constant character
  3. "x" is a constant character
  4. "x" is a plain old character

No

Don't be fooled by the proximity of the * to the char. The * goes with the "w".

No

Don't be fooled by the proximity of the * to the char. The * goes with the "w".

Yes

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

No

The const applies to the whole declaration..


Question 8

What is the name and type of this member function

Set* Set::foo(int i) const
{ ... }

  1. My answer

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


Question 9

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.

  1. *p = 10 ;
  2. *q = 10
  3. p = x;
  4. q = y ;

Not sensible

The const means that "p" can be used to read a variable, but not the change it.

Sensible

q can be used to change int variables

Sensible

"p" itself can be changed

Not sensible

"q" is a const pointer and so can not be changed..


UP

This page was generated by PerlInHTML.