So I call an overloaded addition of two Word
objects with
Word w;
w + w;
the declaration and definition are:
Sentence operator+(const Word&) const;
Sentence Word::operator+(const Word& rightWord) const {
std::cout <<"Entering function Word::operator+(const Word&)."<< std::endl;
std::cout <<"Leaving function Word::operator+(const Word&).\n"<< std::endl;
}
After w + w
is executed, a Sentence
object is destructed (I overloaded the destructor to print to stdout) I created a sentence object earlier, but I don't think that is affecting it. I don't understand how a sentence object is being destructed when it wasn't even constructed (I overloaded the default constructor too).
I also don't understand why it would be created since I'm not even actually returning it. I ran it through gdb and it is definitely calling sentence's destructor when it exits the addition function.