I'm new to c++, so please bear with me if this is a silly question.
The method below seems to create a string named reversePhrase
but with no initial value and then use it phrase.evalPalindrome(reversePhrase)
:
void testForPalindrome(string origPhrase)
{
string reversePhrase;
bool isPalindrome;
Palindrome phrase(origPhrase);
if (phrase.evalPalindrome(reversePhrase))
{
cout << "This phrase IS a palindrome!" << endl;
}
else
{
cout << "This phrase is NOT a palindrome." << endl;
}
cout << "Original phrase: " << origPhrase << endl;
cout << "Reverse phrase: " << reversePhrase << endl << endl;
}
In java this would create a null pointer exception. But I analyzed the method that is being called and it looks like it is accepts the address of a string.
bool Palindrome::evalPalindrome (string& reversePhrase)
{
// code
}
But I don't understand how this is working. Does the initial string reversePhrase;
simply assign memory to reversePhrase
? If yes, how is the calling function able to then print out reversePhrase
that is modified from another function (code is not shown, but it is modified from the other function).
It just seems like writing code this way is hard to read.