-1

Actually,i'm having many doubts regarding string operations.Firstly, I'm confused regarding the use of copy constructor for string concatenation.Is there a need of copy constructor,or just by using parameterized constructor,it can be done.I mean

class string{  
    char*p;  
    int size;  
    public: string(char *a){
        size= strlen(a);  
        p=new char[size];  
        strcpy(p,a);
    }

The above code,initailizes the object dynamically.
how does pointer work if passed as argument in above code,what if i pass char array a[].
Also,the strcpy copies the string.now if i use operator overloading i.e

string operator+(string c)    // i'm defining the function in class definition.  
{
    string t;  
    t.size=size + c.size;  
    t.p=new char[t.size];  
    strcat(t.p,c.p);
    return t;
}

Do i need a copy constructor?and why?
Also can anyone explain me what actually happens when working with pointers to char as in this case.
Secondly,in main() if i declare the objects.Will it be wrong to write

string ob1("Hello world");  

or should i proceed as

char *str;
str="Hello world";

Plz do point out the errors in my code snippet's too,if any.when i'm running the program,it stops in between and it promts program has stopped working.
Why so?

cdhowie
  • 158,093
  • 24
  • 286
  • 300
  • 2
    You do need a copy constructor, and an assignment operator, and a destructor. The compiler-generated defaults will not do the right thing for classes that have exclusive ownership of a raw pointer. – cdhowie Oct 30 '14 at 16:30
  • You are allocating too little memory. Remember: `size == 1 + stlen`. – Deduplicator Oct 30 '14 at 16:36
  • 1
    You need to add 1 to your allocations (for the terminating NULL). You also need a `strcpy(t.p, p);` before the `strcat`in your addition operator. – molbdnilo Oct 30 '14 at 16:36
  • @cdhowie ok,i got that.Thanks for answering.btw,u edited my question,can u plz tell me how to fix those.Actually,i'm not able to put my code and questions properly. – Anshul Sheoliha Oct 30 '14 at 17:08

1 Answers1

2

Do i need a copy constructor?

In general, a class that manages resources does, and you should provide one (along with a constructor and copy-assignment operator) to make the class useful.

However, you shouldn't just for this operator; but your implementation does...

and why?

Because your operator takes its left-hand argument by value, which requires it to be copied. Take it by reference instead:

string operator+(string const & c) const

(It's a good idea for both operands to be const, both to prevent accidental modification, and to allow the operator to be applied to constant values).

However, if you want to fix the memory leak (by deleting the array in the destructor) then you should either provide or delete the copy constructor and copy-assignment operator to avoid double deletion. See this question for the gory details of making a type copyable.

string ob1("Hello world");  

This might fail (with modern compilers) since your constructor takes a non-const char*, while the string literal decays to const char*. You should change the constructor argument to const char* to match (and, in general, always make pointers and references const when you don't need to use them for modification).

Community
  • 1
  • 1
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • @JerryCoffin: See the last sentence. – Mike Seymour Oct 30 '14 at 16:34
  • And ditto for the default assignment operator -- the compiler-generated default is going to cause double deletion. – cdhowie Oct 30 '14 at 16:34
  • The last sentence is better than nothing, but it seems to me that it could be read as indicating that this is something on the order of an optional, possibly-useful extension, when in fact it's basically a necessity for the class to be useful/usable at all. – Jerry Coffin Oct 30 '14 at 16:37
  • @JerryCoffin: I was trying to answer the question, not digress too far into a lecture about class design. I've included a link to the full lecture. But I've added a bit more verbiage to try to hammer the point home. – Mike Seymour Oct 30 '14 at 16:39