String operator+(const String &s)

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

Recommended Answers

All 5 Replies

can you explain more?

That's what the link is for. I suggest you read what that site has to say.

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.

commented: Good points +5
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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.