Re: cString operator= with same buffer
Commit Message
Holger Brunn wrote:
>>Is this the complete patch now? I still suggest adding a copy ctor (which
>>should word like operator= without freeing)..
>
>
> The copy constructor is added with the patch from an older posting
> (vdr-cstring-copyctor.diff).
Just to make sure I'm not missing anything, here's
what I've adopted now:
Klaus
Comments
Klaus Schmidinger on Saturday 26 November 2005 15:14:
> Just to make sure I'm not missing anything, here's
> what I've adopted now:
Hello Klaus,
that's just what the discussion lead to - thanks for your consideration!
Next time I'll provide a kind of "conclusion-patch" to make things easier,
sorry that I didn't this time...
Greetings
Holger
@@ -75,6 +75,7 @@
char *s;
public:
cString(const char *S = NULL, bool TakePointer = false);
+ cString(const cString &String);
virtual ~cString();
operator const char * () const { return s; } // for use in (const char *) context
const char * operator*() const { return s; } // for use in (const void *) context (printf() etc.)
@@ -527,6 +527,11 @@
s = TakePointer ? (char *)S : S ? strdup(S) : NULL;
}
+cString::cString(const cString &String)
+{
+ s = String.s ? strdup(String.s) : NULL;
+}
+
cString::~cString()
{
free(s);
@@ -534,6 +539,8 @@
cString &cString::operator=(const cString &String)
{
+ if (this == &String)
+ return *this;
free(s);
s = String.s ? strdup(String.s) : NULL;
return *this;