Path: utzoo!utgpu!watserv1!watmath!att!dptg!ulysses!andante!alice!ark From: ark@alice.UUCP (Andrew Koenig) Newsgroups: comp.lang.c++ Subject: Re: Getting a string class to work. Message-ID: <10615@alice.UUCP> Date: 22 Mar 90 23:26:57 GMT References: <1759@geocub.greco-prog.fr> <1760@geocub.greco-prog.fr> Organization: AT&T Bell Laboratories, Liberty Corner NJ Lines: 84 In article <1760@geocub.greco-prog.fr>, anthes@geocub.greco-prog.fr (Franklin Anthes) writes: > typedef unsigned long StringLenType; > class String { > char *buf; > StringLenType len; > public: > String(); > String(const char *); > String(const String &); > ~String(); > String& operator=(const String&); > friend String& operator+(String&,String&); > } operator+ should probably take const String& arguments and return a String result, not a String&. In order to return a String&, there has to be some pre-existing String to which to bind the reference. Since the purpose of operator+ is to create a new String, it is hardly likely that an appropriate String exists. So, without reading further, I would change the declaration to friend String operator+(const String&, const String&); > String& String::operator=(const String& s) { > len = s.len; > delete buf; > buf = new char[len+1]; > strcpy(buf,s.buf); > return *this; > } Almost right. This fails if you assign a String to itself, though. Try it this way: String& String::operator=(const String& s) { char* newbuf = new char[len+1]; len = s.len; strcpy(newbuf,s.buf); delete buf; buf = newbuf; return *this; } > String& operator+(String& s1,String& s2) { > String*& ret = new String( s1.len + s2.len ); > memcpy(ret->buf,s1.buf,s1.len); > memcpy(ret->buf+s1.len,s2.buf,s2.len+1); > return *ret; > } Oops! You allocated memory for `ret' but never freed it, as I suggested earlier might happen. Also, I don't understand how `new String(s1.len + s2.len)' can work unless you have String(int) defined to allocate that much memory, which doesn't appear in your declaration. Try it this way: String operator+(const String& s1,const String& s2) { String ret; ret.buf = new char[s1.len + s2.len + 1]; memcpy(ret.buf,s1.buf,s1.len); memcpy(ret.buf+s1.len,s2.buf,s2.len+1); return ret; } For this to work, you must have defined String(const String&) correctly. For example: String::String(const String& s) { buf = new char[s.len + 1]; memcpy(buf,s.buf,s.len+1); len = s.len; } > Now the code above "works", but the space allocated by operator+ is not > deleted. Is there an elegant way to do this sort of thing in C++? The strategy outlined above should avoid all memory leaks. -- --Andrew Koenig ark@europa.att.com