Hi !

Just working to learn some C++ and even though this program worked I failed to understand the underlying concept.......


This is snippet from a program ..... and this is an overloaded "+" operator.... CmyString is a class with two members a string m_pstr and an integer length (length of the string)....pretty simple layout....

my first question why is the function parameter "const CmyString& b" and not just "const CmyString b"


Code:
CmyString CmyString::operator+(const CmyString& b)
{
	CmyString temp;
	temp.length = this->length + b.length;
	delete [] temp.m_pstr;
	temp.m_pstr = new char [temp.length + 1];
	strcpy(temp.m_pstr, this->m_pstr);
	strcat(temp.m_pstr, b.m_pstr);
	
	return temp;
}
I also had a overlaoded "=" operator in the program

Code:
CmyString& CmyString::operator=(const CmyString& b)
{
	if(this != &b)
	{
		length= b.length;
		delete [] m_pstr;
		m_pstr = new char[strlen(b.m_pstr) + 1];
		strcpy(m_pstr, b.m_pstr);
	}

	return *this;
}
So lets say there are three objects :

Cmystring a;
Cmystring b;
Cmystring c;

A statement like

c = a+b;

would execute from left to right so would be

c.operator=(a.operator+(b));

but the object returned by a.operator+(b) will be destroyed... or will it not be ..... ????? I think I got confused in the concepts or refrence and pointers somewhere ......