Reference question

So I was reading my book and it listed this piece of code. The first piece of code is in the book and the 2nd is just my test on the piece of code. I am curious as to why in the functions parameters there is a reference to aString.
I've noticed that removing it has no affect on the outcome of the code.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

bool isPalindrome (string & aString)
{
    string temp;
    temp = aString;
    reverse (temp.begin(), temp.end());
    return temp == aString;
}


int main()
{
    string palindrome = "racecar";
    cout << isPalindrome(palindrome)<< endl;

}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

bool isPalindrome (string aString)
{
    string temp;
    temp = aString;
    reverse (temp.begin(), temp.end());
    return temp == aString;
}


int main()
{
    string palindrome = "racecar";
    cout << isPalindrome(palindrome)<< endl;

}

Last edited on
Hi,

I think you mean removing the reference, not the parameter.

Objects which are of a type of class are usually passed by reference because they could be arbitrarily large, we want to avoid copying the whole thing. This is the concept of pass by reference versus pass by value (copy)

References are just like pointers, except that references must refer to an object, unlike pointers which can be made to point at anything (maybe garbage) including nullptr.

Hope all is well :+)
That makes sense. Thank you TheIdeasman and you were correct in what I was referring to.
Topic archived. No new replies allowed.