Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String concat using overloaded operators.

Status
Not open for further replies.

0x4B47

Programmer
Jun 22, 2003
60
US
Hi fellow coders,

Consider the class,
Code:
class String
{
public:
	String( const char* = "" );
	String( const String & );
	~String();

	const String& operator=( const String & );
    const String& operator+( const String & ); 
private:
	int length;
	char* pStr;
	void setString( const char* );	// Utility function
};

The problem is, how do I implement operator+ so that I can do this:

Code:
void main()
{
   String obj1, obj2("Hello"), obj3(" World");
   obj1 = obj2 + obj3;
   cout << obj1; // Should be "Hello World"
   cout << obj2; // Should be "Hello"
   cout << obj3; // Should be " World"
}

Without modifying obj2 or obj3?

Things you can assume:
- setString dynamically allocates memory for string passed through the constructor, ie pStr = new char blah blah
- ostream overloaded for stream extraction with String objects.

Please help me out.
Thanks in advance.
 
Well since you pass the operator const String & I'd assume you cant modify them as of today...

Code:
// Note: Not returning a ref, but a new object.
String operator+( const String & other)
{
  std::string s = std::string(pStr)+std::string(other.pStr);

  return String(s.c_str());
}

/Per
[sub]
&quot;It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure.&quot;[/sub]
 
Some addition: don't declare your assignment op return value (ref to this) as const. You modify it by assignment - what for const now?..
 
Is it ok for me to do this:
Code:
String operator+( const String & other)
{
  length = this->length + other.length;
  const char* s = this->(pStr-1) + other.pStr;
  setString(s);
  return *this;
}

KG

01001011 01000111
 
oh no I can't do that because 'this' refers to the String object left of the operator doesn't it?
I'm sorry, I'm not in front of a compiler right now thats why I posted the above before giving it a try first. I've been thinking about it all day and this forum is the only thing I can access to maybe share my thoughts with.
How about:
Code:
String operator+( const String & other)
{
  const char* s = this->(pStr-1) + other.pStr;
  String concatStr(s);  
  return concatStr;
}

KG

01001011 01000111
 
Code:
const char* s = this->(pStr-1) + other.pStr;
This is not string concatenation! It is some odd addition of pointers. Use strcat to concat. strings.

Assuming you have some utility method to given a length allocate the pStr:
Code:
String operator+( const String & other)
{
  String result;
  result.allocate(length+other.length); // Alloc enough for 'this'+other
  strcpy(result.pStr, pStr); // result='this'
  strcat(result.pStr, other.pStr); // result+=other
  return result;
}

/Per
[sub]
&quot;It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure.&quot;[/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top