I am trying to write my own C ++ string class for educational and essential purposes.
The first thing is that I do not know much about operators and so I want to learn them. I started writing my class but when I run it, it blocks the program, but does not make any accident.
Take a look at the following code before reading it:
class CString {private: four * cstr; Public: CString (); CSTing (four * str); Cstirring (cssatting and string); ~ Cstring (); Operator four * (); Operator Conceir * (); CSTing Operator + (Const. Cstring & Q) Const .; CSTIR operator = (Const. CSTering and Q); };
At first I am not so sure that I said everything right. I tried to googleing about it, but all the tutorials about overloading explain basic ideas which are very simple but there is a lack of explaining how and when everything is said for example in my = operator in the program Is called CSTIring (CSTIR & FR); But I do not have any ideas.
I have also attached the CPP file:
CString :: CString () {cstr = 0; } CString :: CString (char * str) {cstr = new char [strlen (str)]; Strcpy (cstr, str); } CString :: CString (CString & amp; q) {if (this == & amp; q) returns; Cstr = new four [strlen (q.cstr) +1]; Strcpy (cst, q.cstr); } CString :: ~ CString () {delete (if cstr) [] cstr; } CString :: Operator char * () {return cstr; } CString :: operator const char * () {return cstr; } CString CString :: operator + (const CString & amp; q) const {CString s; S.cstr = new four [strlen (cstr) + strlen (q.cstr) +1]; Strcpy (s.cstr, ctr); Strcat (s.cstr, q.cstr); Return s; } CString CString :: operator = (const CString & amp; q) {if (this! = & Amp; q) {delete (if cstr) [] cstr; Cstr = new four [strlen (q.cstr) +1]; Strcpy (cst, q.cstr); } Return * This; }
for testing I just used a code simple as this cstring = cstring ("hello") + cstring ("world") ;
printf (a);
I tried to debug it, but at one point I lost it before it calls Constructor 2 times for "Hello" and "World". Then it gets in the + operator which is fine. Then it calls the constructor for the empty string. After this, it is found in "Seatring (CSS & ARR)" and now I am lost, why is this happening? After this I saw that my string in which "Hello World" is in ruin (many times in a row). Again I am very upset. Again, after sealing it four seasons and then turning it off it never gets in the operator, but it does not go further. Printf (a) is never reached.
I use VisualStudio 2010 for this, but this is basically just standard C ++ code and this way I do not think it should make a difference of
line:
cstr = new char [strlen (str)];
should be:
cstr = new char [strlen (str) + 1]; In addition, there is no point in the test copy constructor for self-assignment - you are creating a new object - this can not possibly be an address similar to an existing object and copy The manufacturer should refer to a conference reference as a parameter, If in your code, you were hoping to use the assignment operator, then you would expect a wrong. This code:
CString a = CString ("Hello") + CString ("World");
is essentially the same:
CString a (CString ("Hello") + CString ("World"));
The copy is manufactured, the assignment is not. Temporary cstring "Hello world" will be destroyed (the destroyer applied) has been constructed after one.
Actually, it sounds as if you are working as less or more expected of your code.
Comments
Post a Comment