# C++ Workshop - Week 8-ish (Ch. 9)

This topic is 3799 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Chapter 9 – Exploiting References

Introduction Jeromy has had a couple issues with his computer and has been absent for a bit, so I'll pitch in with the next weeks thread. You've had a couple week break to absorb and practice with pointers, so hopefully this week will be a bit easier. Pointers have many uses in modern programming and can make very efficient code. References are a new addition to c++ that have some of the same advantages as pointers, but allow for an easier syntax. If you have a good grasp on pointers (which hopefully you do after the last chapter) references should not be too complicated to grasp. For Tutors Please remember to use OPINION and WARNING tags whenever applicable. As well, feel free to post your own insights, and review questions or exercises beginning Wednesday or Thursday. Outline of the Reading - Chapter 9
1. What is a Reference?
2. Using the Address-of operator (&) on references?
3. Referencing Objects
4. Null Pointers and Null References
5. Passing Function Arguments by Reference
6. Understanding Function Headers and Prototypes
7. Returning Multiple Values
8. Passing References for Efficiency
9. Knowing When to Use References vs. Pointers
10. Mixing References and Pointers
11. Returning Out-of-Scope Object References
12. Pointer, Pointer, Who Has the Pointer
Additional Resources None... Weekly Errata None...

## Good Luck!

##### Share on other sites
Heya All!

Sorry its taken me so long to get back to you guys. My computer melted a few weeks ago, and I've been trying to get back online since then. With my 1 mo. baby at home I cant get out much.

References are rather easy, and are a logical follow-through from pointers, so we'll just donate a half of a week to it so we can get back on schedule. I will try and post some quizzes, etc...over the next few days to help people get back in the mood.

Also, the first project will be posted on Monday, and will be running until the 15th of September, so be looking for that.

Cheers!

##### Share on other sites
A small point which is about consistency in the book. I don't know if this problem is related to different linkers/compilers:

On page 256, in the NOTE section, it states that there must be a space before the reference operator but the space following the reference operator is optional.

In question 6 of the quiz on page 287, it asks which of a, b and c is correct and the answer is given that all are acceptable.

I've played around with line 9 of listing 9.1, varying the spacing before and after the reference operator and it compiles/runs correctly whether it's int&rSomeRef, int &rSomeRef or int & rSomeRef. I use MSVC++ Express Edition.

Is the comment in the NOTE section wrong, or does this depend upon which software is used to link/compile?

##### Share on other sites
Though I can't claim that there are no compilers that don't handle it correctly, I am fairly confident in my claim that the standard says the space doesn't matter. (The note is wrong.)

Or the note may have actually been refering to the address of operator, and comparing this new usage to the one you learned in the last chapter. Read that note again, and let us know.

##### Share on other sites
The note says:

"Note that the reference operator (&) is the same symbol as the one used for the address-of operator. These are not the same operators, however, although clearly they are related.

The space before the reference operator is required; the space between the reference operator and the name of the reference variable is optional. Thus

int &rSomeRef = someInt; // ok
int & rSomeRef = someInt; // ok"

It appears that the note is wrong.

##### Share on other sites
int& rSomeRef = someInt; // also ok and in fact preferred by many

It's not possible to use the address-of operator in the type section of a declaration/definition, so there's no ambiguity with address-of.

##### Share on other sites
Indeed, the areas where the two operators appear are mutually exclusive. The & (reference) operator isn't actually an operator; it's a type-modifier, not unlike 'const' or 'volatile', and it can appear only in a declaration. The & (address-of) operator can appear only in a statement. This relationship is the same one shared between the * (pointer) type modifier and * (dereference) operator.

& (binary and) and * (multiplication) are both binary operators, and are thus differentiated from & (address-of) and * (dereference) as well.

For that matter; the only operator that can be used in a declaration is :: (scope-resolution) anyway. I think. I'm not going to put money on that statement. :)

##### Share on other sites
I justed wanted to thank everyone on the workshop. tutors and students included. I've learnt a lot here and although I havent posted yet I have been following closely. This had been a valuable source of info for me for what we have learnt up to this point and I consider myself getting old at 33 I'm still enjoying learning (J/K about the age thing, my parents keep calling me telling me I'm too old for games ;) ). Its been a reresher for me as I did learn c++ at uni but have never practiced it so forgot all about it. (I've been programming in C#...)

I believe I can make a very simple game which i intent to post shortly using the things we have currently learnt :) Keep up the good work and I look forward to continued reading.

##### Share on other sites
Greetings All!

Yeah, ok, so I know I'm WAAAAY behind on quizzes, but I've been busy with work and the new baby. And I'm actually rather surprised that no one else following the workshop bothered to create a quiz. At any rate, I finally got caught up. So....

It's once again QUIZ TIME!!! That's right, listed below are a set of quiz questions to help you test your knowledge and understanding of the material contained in chapters 9.

In addition to the questions and exercises below, make sure that as you're reading the book you enter the examples into your compiler, build the program, and run the executable. I know this is a time consuming process, but the repeat use of keywords, syntax, and semantics will help ingrain the information into your long-term memory. My advice is to create a simple "driver" project with a function main. As you read, enter the examples into function main, test it, and then erase it for use again in the next example.

I will create an answer thread for these questions immediately, so that people will have a chance to get the answers more quickly.

Chapter 9 Quiz

1. What is a reference?
2. What is returned if you try and get the address of a reference?
3. Can references be reassigned?
4. Can you create a reference to a class?
5. Is it possible to have null references?
6. What happens when a function prototype requests a reference.
7. How does the client (the caller) of a function know that the function uses references? (where can the client look)
8. What’s one method of allowing a function to “return” more than 1 value.
9. What benefits are there for using const references as parameters to functions rather than allowing the compiler to make copies of the objects? When is this most useful?
10. According to the text, when is the best time to use references? When is the best time to use pointers?
11. What is the primary thing to be careful of when returning references from a function?
12. According to the author, is it a good idea to allocate memory within a function, if the primary purpose is to return the newly allocated object back to the caller? If not, what’s an alternate solution?

##### Share on other sites
[EDIT: Question posted in programming forums]

[This has some to do with referencing]
I have mulled this topic around in my head some, and I have yet to figure it out: What are the specifics of returning an object by a function (non-reference)?

The reason why I question/ask this is that I have written a string class (for practice, not meant to be better than STL's), and I have been confused as to how to create an efficient concatenation method.
I have this code:
class string { ... string& operator+=(const string &x) { ... } //With overloads for 'char' and 'char*'};string operator+(const string &lhs,const string &rhs) { string temp(lhs); return temp+=rhs;}

However, I question it's efficiency. I feel as though I'm copying strings more than needed.
The contextual question is: Is the function returning 'temp', or is it returning a string copying temp?
[Following questions assume latter is true] How could I get rid of this excess copying? Is there a way to access the return variable directly? [Iffy, I know] Would inline-ing 'operator+' do anything, or would it still copy again?

NOTE: I know that this does not incur enough processing to relatively slow anything down, but I would be more at ease if there was something more I could do.

[Edited by - deadimp on November 17, 2006 7:06:13 PM]

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 11
• 14
• 9
• 9
• 9
• ### Forum Statistics

• Total Topics
632924
• Total Posts
3009230
• ### Who's Online (See full list)

There are no registered users currently online

×