Re: cString operator= with same buffer

Message ID 43886DBD.8090403@cadsoft.de
State New
Headers

Commit Message

Klaus Schmidinger Nov. 26, 2005, 2:14 p.m. UTC
  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

Holger Brunn Nov. 26, 2005, 2:57 p.m. UTC | #1
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
  

Patch

--- tools.h     2005/11/05 10:54:39     1.83
+++ tools.h     2005/11/26 14:03:47
@@ -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.)
--- tools.c     2005/11/04 16:33:18     1.103
+++ tools.c     2005/11/26 14:12:31
@@ -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;