I'm kind of confused about references in C++. Could anyone explain me this :
class Dog
{
public:
Dog( void ) {
age = 3;
name = "dummy";
}
void setAge( const int & a ) {
age = a;
}
string & getName( void ) {
return name;
}
void print( void ) {
cout << name << endl;
}
private:
int age;
string name;
};
int main( void ) {
Dog d;
string & name = d.getName(); // this works and I return reference, so the address of name will be the same as address of name in Dog class.
int a = 5;
int & b = a; // why this works? by the logic from before this should not work but the code below should.
int & c = &a // why this does not work but the code above works?
}
Also when i remove &
and make the function like this string getName
the code won't work.