String operator+(const String &s)

Here String is a class. here why we are using ampersand operator?
please explain me.(&s ?)

Also, the form of your example:

String operator+(const String &s)

suggests that operator+ is a member function. The name of the class, String , suggests that operator+ is probably being used for concatenation. The fact that the function has one argument rather than two suggests that it is probably a member function rather than a standalone function. Using a member function for concatenation is a bad idea because it requires the left operand of a concatenation to be a String rather than a type that can be converted to String .

Moreover, the lack of const in the member-function declaration (if, indeed it is a member function) means that you cannot use a const String object as the left operand.

That's two elementary programming errors in that one example. If you saw that example in a textbook, it may be a good idea to consider a different textbook.

Edited 6 Years Ago by arkoenig: n/a

Comments
Good points
int String::CompareNoCase(const String& pData)
{	
	int nIndex  = 0; // for increment the position
	int nLen    = 0; // for store length

	// if string is null
	if(pData.m_pText != NULL)
	{
		// length of string
		nLen = (int) strlen(pData.m_pText);
	}

	// check operand string is less or not
	if(m_nLength < nLen)
	{
		return (NEGATIVE_ONE);
	}
	else if(m_nLength > nLen)
	{
		return (NOT_EQUAL);
	}

	// for check equality
	for(nIndex = 0; nIndex < m_nLength; nIndex++)
	{
		// check char is equl or not
		if(toupper(m_pText[nIndex]) != toupper(pData.m_pText[nIndex]))
		{
			if(int(m_pText[nIndex]) > int(pData.m_pText[nIndex]))
			{
				return (NOT_EQUAL);
			}

			// if operand string is less then parameter string return -1
			return (NEGATIVE_ONE);
		}
	}

	// return if strings are equal
	return (EQUAL);
}


How can i create lexicographical comparison function for string?
what modification i want to do in this function?

Edited 6 Years Ago by sabareesh: n/a

This article has been dead for over six months. Start a new discussion instead.