Here is my code for my logical comparison operator (==) overloaded. I use this to check if two strings are identical in size and content. It should return false otherwise.
bool MyString::operator==(const MyString& other)const
{
if(other.Size == this->Size)
{
for(int i = 0; i < this->Size+1; i++)
{
if(&other == this)
return true;
}
}
else
return false;
}
When I ran valgrind it told me warning control reaches end of non-void function. Any suggestions on how to fix this issue and what I could do to better the code?
When control reaches the end of your
forloop, you immediately get to the end of the function without returning a value.It looks to me like you have the logic in your
forloop munged anyway — it’s comparing the address of the other item to this. While it’s sort of okay to do that, you only need to do it once, not in a loop.In the loop, you undoubtedly want to compare the characters in the string, not the addresses of the objects.
Edit:
A typical implementation would be something on this general order: