Loading...

C++ References

The headers page left one line on the screen after Lab::mark(); with lab.h included into box.cpp. This page opens the Memory and Collections module. The workshop file on disk stays box.cpp. The new work gives a second name to one string variable so a change through that name updates the original. The screen still shows one line: pine.
A reference is another name for an existing variable that already lives in memory. On this page you write std::string word = "cone"; and then std::string& alias = word;. The & in the type means alias is not a separate string copy sitting beside word. Then alias = "pine"; changes word as well, because both names share one place. Printing word shows pine on the screen. Reading std::string& alias = word is reading a bind to the same storage, not a second independent value with its own copy of the text.
Programs use references so a function or a later line can work on the caller's variable without copying it first. You do not need lab.h on this page, and you do not keep the Lab namespace body as the lesson path. Creating a pointer with * waits for the next page in this module. Here the point is only the & on the type and the bind at the moment you declare alias. The build command stays familiar: one source file box.cpp, one output name box, one run that prints pine. Keep #include <iostream> and #include <string> together at the top. You compile only box.cpp again for this workshop file.

Above or inside main, you only need main on this page. Keep #include <iostream> and #include <string>. Inside main, write std::string word = "cone";, then std::string& alias = word;, then alias = "pine";, then std::cout << word << std::endl;, then return 0;. Do not leave alias unbound. Do not write std::string alias = word; without & as the lesson path, because that would copy.
Walk the run carefully from the first line of main. The program starts at main. word starts as cone. alias becomes another name for that same string storage. alias = "pine"; stores pine into that shared storage. Printing word shows pine on the screen. Then main returns and the program ends. There is one line on the screen. It is pine, not cone. If you printed alias instead, you would still see pine, because both names share the same value after the assignment. The checked file prints word on purpose so the original name proves the change went through the reference.

If you write std::string& alias; with no bind, the build fails because a reference must refer to something when it is created. That failed experiment stays in the prose. The checked file always binds alias to word on the same line.
If you write std::string alias = word; without &, then alias = "pine"; leaves word as cone. The checked file keeps the & so both names stay tied.
A common mistake is to put the & on the value like alias = &word in this beginner shape. Another common mistake is to rebind alias to a second variable later; a reference stays bound to the first object. A third mistake is to skip the first value and never show that cone was replaced. A fourth mistake is to print only a literal "pine" and never use the reference. A fifth mistake is to drop #include <string> after bringing std::string back.
This page stops before the next tool, where a pointer holds an address with *. The next page names that tool once at the end. The reference you have just written only shows a second name for one variable in the same function. Keep the bind on the declaration line on purpose so the meaning stays clear for a first reading.

Open box.cpp. You can remove the lab.h include and the Lab namespace for this page. Keep iostream and string. In main, create word, bind alias, set alias to pine, and print word. The file name on disk stays box.cpp. The checked file is below, and the new lines are the reference bind and the assignment through alias.

box.cpp binds alias to word then prints pine

cpp
#include <iostream>
#include <string>

int main()
{
    std::string word = "cone";
    std::string& alias = word;
    alias = "pine";
    std::cout << word << std::endl;
    return 0;
}

Rebuild with g++ -std=c++17 box.cpp -o box, then run ./box. If g++ is missing and clang++ is the compiler you installed, swap only the tool name: clang++ -std=c++17 box.cpp -o box. The screen shows one line: pine.
Check that line first on the screen. Changing alias changed word through the shared storage. The table gives each row its piece, so compare your run with it line by line before you move on. If the screen shows cone, the & may be missing and you copied instead of binding. If the build fails about an uninitialized reference, bind alias when you declare it on that same line.

📊 reference and what the screen shows

Piece What the screen shows
First pine, because alias = "pine" updates word through the reference
Last pine, because the program prints one line and stops
Rule a reference is another name for an existing variable
Screen pine

Try one change at a time. Change alias = "pine"; to alias = "oak";, leave the bind as it is, and rebuild. The screen shows oak instead of pine, because the reference still updates word. Put "pine" back. Next, temporarily remove the & so the line reads std::string alias = word;, assign "pine" to alias, print word, and rebuild to see cone again. Restore the & so the file returns to the checked shape. Each experiment changes one thing on purpose, so the screen shows exactly whether the names shared storage. Undo each change before the next one starts, so the file always returns to the checked workshop version with the & bind fully restored once again.
This page ends the references tool and stops before the next one. The next page is pointers, which stores an address with *. The reference you have just written only shows a second shared name for word in box.cpp. You leave with one line on the screen: pine. Count the lines before you leave. There is exactly one printed line on that final screen, and that single printed line on the screen is still just pine alone right there now after the final build.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
4
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top