Jump to content
  • Advertisement
Sign in to follow this  
Sr_Guapo

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.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Welcome to the GDNet C++ Workshop – Ch. 9

For a complete introduction to this workshop, please look here. As well, although this thread is temporarily mirrored in the "For Beginners" forum for easy access, the thread and workshop is actually located in a separate forum called "CPP Workshop" located Here. You can also find it on the main Forums page at the VERY bottom. Workshop Overview This workshop is designed to aid people in their journey to learn beginning C++. This workshop is targeted at highly motivated individuals who are interested in learning C++ or who have attempted to learn C++ in the past, but found that without sufficient support and mentoring they were unable to connect all the pieces of this highly complex but powerful programming language. This is a 'guided' self-teaching C++ workshop. Each student is responsible for taking the time to read the material and learn the information. The community and tutors that arise out of this workshop are here for making the learning process run more smoothly, but are not obligated to baby-sit a person's progress. Because everyone will be working from the same textbook (Teach Yourself C++ in 21 days 5th Ed.), students may find it easier to get answers to the specific questions they might have. There is no minimum age requirement, and there is no previous programming experience required. Additionally, this workshop does not attempt to defend C++ as a language, nor does it attempt to demonstrate that C++ is either more or less useful then other programming languages for any particular purpose. People who intend to start a discussion about the differences between C++ and ANY other languages (except as are relevant to a particular discussion), are encouraged to do so elsewhere. This workshop is for educational, not philosophical discussions. Tutors For an in-depth introduction to our tutors, along with the official list, as well as information on how to apply to be a tutor, look here. Over the duration of the roughly 21 weeks of the C++ Workshop there will be many questions asked and (hopefully) many questions answered. Unfortunately, not all answers are created equal. People tend to respond to questions for a number of reasons, not all of which are entirely self-less, and can sometimes even be dangerous to the learning process of those trying to absorb the information. Keep in mind that although your peers on the forum may be anxious and excited to help you, many of them may be learning the information for themselves, and not in a position to claim mastery of the subject. For this reason we’ve decided to establish a list of people we feel to be qualified tutors. People on our list are people we feel have attained a level of mastery such that their answers can generally be accepted as fact. Refer to the link above for the list of workshop tutors. Quizzes & Exercises Each week will have quizzes and exercises posted in the weekly threads. Please try and answer them by yourself. As well, please DO NOT post the answers to Quizzes and Exercises within this thread. Once it becomes acceptable to post the answers to quizzes and exercises, an additional thread will be created each week specifically for the purpose of posting quiz answers. If you try with reasonable effort but are unable to answer the questions or complete the exercises, feel free to post a clarification question here on the thread. Tutors, myself, or others will do the best we can to point you in the right direction for finding the answer.

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 this post


Link to post
Share on other sites
Advertisement
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.

Please post any questions you have on the chapter about References in this thread as usual.

Cheers!

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

PLEASE DO NOT POST THE ANSWERS TO THESE QUESTIONS OR EXERCISES. If you are unable to answer these questions, please ask for assistance, but DO NOT POST THE ANSWERS. Any question which is not marked with [Extra Credit] can be answered by reading your textbook. Questions which are marked [Extra Credit] either have been answered in the thread previously, or can be answered by doing a bit of research.

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 this post


Link to post
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]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!