< Previous | Contents | Next >

Questions and Answers

Q: Different programmers put the reference operator (&) in different places when declaring a reference. Where should I put it?

A: Three basic styles exist with regard to using the referencing operator. Some programmers opt for int& ref = var;, while others opt for int & ref = var;.

Questions and Answers 219


Still others opt for int &ref = var;. The computer is fine with all three. There are cases to be made for each style; however, the most important thing is to be consistent.

Q: Why cant I initialize a non-constant reference with a constant value?

A: Because a non-constant reference allows you to change the value to which it refers.

Q: If I initialize a constant reference with a non-constant variable, can I change the value of the variable?

A: Not through the constant reference because when you declare a constant reference, youre saying that the reference cant be used to change the value to which it refers (even if that value can be changed by other means).

Q: How does passing a constant reference save overhead?

A: When you pass a large object to a function by value, your program makes a copy of the object. This can be an expensive operation depending on the size of the object. Passing a reference is like only passing access to the large object; it is an inexpensive operation.

Q: Can I make a reference to a reference?

A: Not exactly. You can assign one reference to another reference, but the new reference will simply refer to the value to which the original reference refers.

Q: What happens if I declare a reference without initializing it? A: Your compiler should complain because its illegal.

Q: Why should I avoid changing the value of a variable that I pass through a reference?

A: Because it could lead to confusion. Its impossible to tell from only a function call whether a variable is being passed to change its value.

Q: Does that mean I should always pass a constant reference?

A: No. You can pass a non-constant reference to a function, but to most game programmers, this signals that you intend to change the argument variables value.

220 Chapter 6 n References: Tic-Tac-Toe


Q: If I dont change the argument variables passed to functions, how should I get new information back to the calling code?

A: Use return values.

Q: Can I pass a literal through a non-constant reference?

A: No. If you try to pass a literal as a non-constant reference, youll generate a compile error.

Q: Is it impossible to pass a literal to a parameter that accepts a reference? A: No, you can pass a literal as a constant reference.

Q: What happens when I return an object from a function?

A: Normally, your program creates a copy of the object and returns that. This can be an expensive operation, depending on the size of the object.

Q: Why return a reference?

A: It can be more efficient because returning a reference doesnt involve copying an object.

Q: How can I lose the efficiency of returning a reference?

A: By assigning the returned reference to a variable. When you assign a reference to a variable, the computer must make a copy of the object to which the reference refers.

Q: Whats wrong with returning a reference to a local variable?

A: The local variable doesnt exist once the function ends, which means that youre returning a reference to a non-existent object, which is illegal.