I’m teaching myself C++, and in the process I’m writing simple little programs to learn basic ideas. With respect to “pass-by-reference”, I’m confused why the following piece of code works (some of the code is just there to practice overloading constructors):
#include <iostream>
#include <string>
using namespace std;
class Dude
{
public:
string x;
Dude(); // Constructor 1
Dude(const string &a); // Constructor 2
};
Dude::Dude() : x("hi") {}
Dude::Dude(const string &a) : x(a) {}
int main()
{
Dude d1;
Dude d2 = Dude("bye");
cout << d1.x << endl;
cout << d2.x << endl;
return 0;
}
In “main()”, I create an object “d2” of type “Dude”, and use Constructor 2 to set “x” to be the string “bye”.
But in Constructor 2’s declaration, I told it to accept an address of a string, not a string itself. So why can I pass it “bye” (which is a string). Why don’t I have to create a variable string, and then pass the address of that string to Constructor 2 of Dude?
This actually illustrates one of the coolest and most useful features of C++: Temporary variables. Since you specified that the string reference is
const, the compiler allows you to pass a reference to a temporary value to that function. So, here’s what’s happening behind the scenes withDude d2 = Dude("bye");:Dude::Dude(const string &). How this choice is made is a whole different topic.stringvalue. Now,"bye"is aconst char[4], but the compiler can trivially convert that to aconst char *, and that can be turned into astring. So, an anonymous temporary variable (call ittemp1) is created.string::string(const char *)is invoked with"bye", and the result is stored intemp1Dude::Dude(const string&)is invoked with a reference totemp1. The result is assigned tod2(actually, it is assigned to another temporary variable and the copy constructor forDudeis invoked with a const reference to it and that is assigned to d2. But in this case the result is the same.)temp1is discarded. This is where the string destructorstring::~string()is run ontemp1