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?