const string& -- what does '&' signify?

class Cat : public Animal
{
public:
Cat(const string& name) : Animal(name) { }
virtual const string talk() { return "Meow!"; }
};


what does the '&' signify in 'string&'. Why not just say "Cat(const string name)... ??
reference.
A function has a const TYPE& argument so that is avoided the creation of a new instance and the constructor isn't called

http://www.cplusplus.com/doc/tutorial/functions2.html
I'm an old C programmer trying to learn C++, and I was used to the old C style of passing by reference using *. I actually like the C++ syntax much better, it's so much cleaner, but if I use the old C style is that severely frowned upon in C++ circles?

#include <stdio.h>

void swapnum(int *i, int *j) {
int temp = *i;
*i = *j;
*j = temp;
}

void swapnumCPP(int& i, int& j) {
int temp = i;
i = j;
j = temp;
}

int main(void) {
int a = 10;
int b = 20;
printf("before swap: A is %d and B is %d\n", a, b);
swapnum(&a, &b);
printf("after swap: A is %d and B is %d\n", a, b);

printf("before swapCPP: A is %d and B is %d\n", a, b);
swapnumCPP(a, b);
printf("after swapCPP: A is %d and B is %d\n", a, b);
return 0;
}
They are both OK, the only advantage of using reference is having slightly less typing
In my world I wouldn't say "severely", but I would frown upon it.
Topic archived. No new replies allowed.