C++ this
The constructors page left one line on the screen in box.cpp: pine, after Tag t("pine"); and t.show(), with word = w; inside the constructor. This page stays in the same file and the same Classes module. The type name Tag, the member word, and show stay. The new work uses this so the constructor can name the member even when the parameter uses the same word. The screen still shows one line: pine.
The word this names the current object from inside a member. On this page the constructor parameter is also called word, matching the member. A bare word on the left would mean the parameter, not the member. You write this->word = word; so the left side is the member on the object and the right side is the parameter value. The Classes cast keeps Tag and box.cpp. The value pine is still the member on the object; this is the tool that points at that object from inside the constructor. Reading this->word is reading the member that belongs to the object being built.
Programs use this when a parameter name matches a member name, so the store reaches the object. You do not need a different parameter letter on this page. Creating two objects still fills each object through its own constructor call, but this page builds one object so the screen stays one clear line. The next page names member functions that do more work on the object. Here the point is only this-> before the member on the left side of the fill. The build command stays familiar: one source file box.cpp, one output name box, one run that prints pine.
Above main, keep class Tag with std::string word; and show that prints word. Change the constructor header to Tag(std::string word) and inside write this->word = word;. Keep the includes for iostream and string. There is still no second main inside Tag. The constructor still has no return type line. Its name is still Tag.
Inside main, keep Tag t("pine");, then t.show();, then return 0;. The parentheses still pass pine into the constructor. Now the parameter is named word. The line this->word = word; stores pine on the object. The call to show prints that member. You are not writing word = w; with a different parameter letter on this page.
Walk the run. The program starts at main. Tag t("pine"); builds one object and runs the constructor. Inside the constructor, this names that object, so this->word is the member and word on the right is the parameter pine. After the fill, t.show(); prints pine. Then show finishes, main returns, and the program ends. There is one line on the screen. It is still pine. By the time show runs, the member already holds pine because the constructor used this to store it.
If you write word = word; without this-> on the left, the parameter copies onto itself and the member may stay empty, so the screen can look blank. That failed experiment stays in the prose. The checked file always uses this->word = word;.
If you keep the old parameter name w and write this->word = w;, that also works, but this page deliberately matches the names so you need this. The checked file uses matching names.
A common mistake is to write this.word with a dot, which C++ rejects for this pointer form; use the arrow this->word. Another common mistake is to put this only on the right side. A third mistake is to drop this-> and keep matching names, so the member never updates. A fourth mistake is to treat this as a type name in main. A fifth mistake is to remove the constructor and go back to t.word = "pine"; for this page.
This page stops before the next tool, where members do more work as functions on the object. The next page names that tool once at the end. The this fill you have just written only shows how the current object is named from inside the constructor when names match.
Open box.cpp. Keep the includes, word, and show. Change the constructor to Tag(std::string word) with this->word = word; inside. In main, keep Tag t("pine"); and t.show();. The file name on disk stays box.cpp. The checked file is below, and the new lines are the matching parameter name and the this-> fill.
#include <iostream>
#include <string>
class Tag
{
public:
std::string word;
Tag(std::string word)
{
this->word = word;
}
void show()
{
std::cout << word << std::endl;
}
};
int main()
{
Tag t("pine");
t.show();
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. The constructor stored pine through this->word, and t.show printed it. The table gives each row its piece, so compare your run with it line by line. If the screen is blank, the fill may still be word = word; without this->. If the build fails about this.word, switch to the arrow form this->word. If the build fails about a return type on Tag, remove that return type from the constructor header.
📊 this and what the screen shows
| Piece | What the screen shows |
|---|---|
| First | pine, because this->word = word stores pine on the object |
| Last | pine, because the program prints one line and stops |
| Rule | this names the current object so this->word is the member |
| Screen | pine |
Try one change at a time. Change Tag t("pine"); to Tag t("cone");, leave the this->word = word; line as it is, and rebuild. The screen shows cone instead of pine, because only the argument at creation changed and this still filled the member. Put "pine" back. Next, after t.show();, add Tag u("cone"); and u.show();, leave the first object as it is, and rebuild. The screen shows pine, then cone, because each constructor call uses this on its own object. Remove the second object before you leave so the file returns to the checked one-line shape. Each experiment changes one thing, so the screen shows exactly which creation argument was stored through this. Undo each change before the next one starts, so the file always returns to the checked version.
This page ends the this tool and stops before the next one. The next page is member-functions, which gives the object more work to do through members. The this fill you have just written only shows naming the current object when parameter and member share a word. You leave with one line on the screen: pine. Count the lines before you leave. There is exactly one printed line, and it is pine.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 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