That's the standard form of a copy constructor. Allows you to initialise a string from another one.
For example:
1 2 3 4 5 6 7 8 9 10
int toNumber(std::string str)
{
//...
}
int main()
{
std::string text = "43";
int n = toNumber(text); // pass string by value uses copy constructor
}
There are four standard functions that get generated for your class by default. The copy constructor is one, another interesting one is the copy operator that allows you to do assignments, in this case:
The ampersand symbol (&), is almost like a pointer. What it's used for is conserving space because when you put it as a return value int& or an argument myFunction(int&); It will instead refer to the variable it gives/receives instead of creating a new one.
For instance:
1 2 3 4 5 6 7 8 9 10 11 12 13
istream& read(istream& is, Student_info& s)
{
// read and store student's name and midterm and final exam grades
cout << "Write the student's name followed by the midterm and final grades." << endl;
is >> s.name >> s.midterm >> s.final;
cout << endl;
cout << "Write the homework grades for " << s.name << ": " << endl;
read_hw(is, s.homework); // read and store student's homework grades
return is;
}
istream& saves up alot more space, since istream is so much data
The ampersand symbol (&), is almost like a pointer.
A symbol is just a symbol. What you mean is that a type followed by an ampersand denotes a reference, and references are basically pointers which are always dereferenced and can only point to an object (can't be NULL or made to point at another object at runtime).
Incidentally, it's nice to give credit when you present code someone else wrote. Your example comes from Accelerated C++, IIRC.
wow, energizer... that really clarified things. Thanks a lot.
Also, I read one of your post. I was reading the Accelerated C++ book as well. It's super hard to phantom because it does have a lot of boringness to it!
I will post more on what I think about it in your thread to be fair and let others know what I am talking about.
filipe, thanks you also made me do some critical thinking... it's dereferencing, but is that true for argument types that & followsintdo(int& g)... does that mean that normally an argument variable pointer is created for g if the & isn't there... and if it is... why don't we use the dereference variable inside the functions scope when manipulating it?
hmm... thinking about it more is it being followed by the variable g so it must be dereferencing g... correct?
I'm not sure you understand what dereferencing means. Example:
1 2 3 4 5 6 7 8
int n = 5;
int* a = &n; // a now points to n
int& b = n; // b is now another name for n
std::cout << "Value of variable a: " << a << std::endl;
std::cout << "Address of variable n: " << &n << std::endl;
std::cout << "Variable a dereferenced: " << *a << std::endl;
std::cout << "Value of variable b: " << b << std::endl;
// b evalutes to whatever value n holds; it's like a pointer that's always dereferenced
So, if I have a function int foo(int g) (do is a keyword, we can't use it for identifiers), and I call it on n, it will never change the value of n, because it will work with a copy of n, not the original object. If the function is int foo(int& g), it will work directly on n and might change its value. We could say instead int foo(int* g), but we'd have to check for null pointers and we'd have to say *g to refer to the object pointed to by g (in our example, n).