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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GNU string deep copy

Status
Not open for further replies.

stuartd

Programmer
Jan 8, 2001
146
US
Hi,

I am using GNU c++ string class. I have been copying strings in my code :

stringA = stringB;

This seems to do a shallow copy, as the address of the contents of the strings are the same.

How do I do a deep string copy?


Thanks.
 
Well,you could overload the '=' operator to make you a deep copy. I find it strange the class doesn't do it for you... Greetz,

The Muppeteer.

themuppeteer@hotmail.com

Don't eat yellow snow...
 
Actually, if the = is already overloaded he couldn't re overload it... he'd have to re-compile the string class with a new = operator.

What system, version of g++ and is it a call to the operator or the copy constructor?

operator=:
string A = "something";
string B;
B = A;

Copy constructor:
string A = "somehing";
string B = A;

You could always use a string stream, or the copy/copy_back STL functions (either from the class, or from the algorithum library [with iterators]), but I think you should just be able to find a fixed version of the string library for your system. Just some thoughts.
 
Of course you can't overload it again, but if it was already overloaded,I think the least it could do is make a deep copy (basic functionality), so I presumed it wasn't overloaded yet.
I think it uses the copy constructor...
Greetz,

The Muppeteer.

themuppeteer@hotmail.com

Don't eat yellow snow...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top