26-04-2005, 06:12 PM | #1 | ||
Join Date: Jan 2005
Location: ,
Posts: 454
|
Hi, i'm learning the OOP and C++ at home. Now I'm at the Constructor - destructor stuff, and while going thru some exercises i got an error. Now please look at the code and help me. Thanks!
Code:
#include <iostream> #include <conio.h> #include <cstring> using namespace std; class ZnakovniNiz { public: char *pok; ZnakovniNiz(char *p): pok(new char[strlen(p)+1]) { *int i; *for(i=strlen(p)+1;i>=0;i--) * *(pok+i)=*(p+i); }; ZnakovniNiz(const ZnakovniNiz &zn): pok(new char[strlen(zn.pok)+1]) { *int i; *for(i=strlen(zn.pok)+1;i>=0;i--) * *(pok+i)=*(zn.pok+i); }; ~ZnakovniNiz() { delete [] pok; }; }; int main(void) { char strl[]="Tko je taj pokemon?"; ZnakovniNiz PrviNiz(strl); cout<<strl<<endl<<PrviNiz.pok<<endl; ZnakovniNiz DrugiNiz(PrviNiz); cout<<DrugiNiz.pok<<endl; *(DrugiNiz.pok+3)='%'; cout<<DrugiNiz.pok<<endl; cout<<PrviNiz.pok<<endl; getch(); return 0; } At the end of execution i get this problem: I also know that the program worked fine before creating a destructor.
__________________
Never mess with me when I have a cougar, Never! |
||
|
|
26-04-2005, 06:38 PM | #2 | ||
Join Date: Jun 2004
Location: Jan Mayen, Svalbard and Jan Mayen
Posts: 2,167
|
the loops you use to copy a string are flawed.
for(i=strlen(p);i=0;i--) Then it will be allright.
__________________
Flowing with the stream of life |
||
|
|
26-04-2005, 06:50 PM | #3 | ||
Join Date: Jan 2005
Location: ,
Posts: 454
|
Quote:
Code:
for(i=strlen(p);i>=0;i--) * *(pok+i)=*(p+i); Thanks Data! k: Now you have honour off closeing this topic. ...
__________________
Never mess with me when I have a cougar, Never! |
||
|
|
26-04-2005, 07:25 PM | #4 | ||
Join Date: Jun 2004
Location: Jan Mayen, Svalbard and Jan Mayen
Posts: 2,167
|
as you allready include cstring...
why not do a strcpy(pok,p); Oh well That's outside the scope of this thread.
__________________
Flowing with the stream of life |
||
|
|
26-04-2005, 07:50 PM | #5 | ||
Join Date: Jan 2005
Location: ,
Posts: 454
|
Quote:
__________________
Never mess with me when I have a cougar, Never! |
||
|
|
26-04-2005, 08:00 PM | #6 | ||
Join Date: Jun 2004
Location: Jan Mayen, Svalbard and Jan Mayen
Posts: 2,167
|
I doubt it.
the libc functions are quite optimized. (they could have been written in pure asm). Don't try to reinvent the wheel. as there smarter systems for copying a string. like while ((a[i]=b[i])) i++; and not to mention that strlen takes computing power as well. (strcpy probably doesn't check for the length.(like that while system.)
__________________
Flowing with the stream of life |
||
|
|
26-04-2005, 08:08 PM | #7 | ||
Join Date: Jan 2005
Location: ,
Posts: 454
|
Cool, great man....
...so can i PM you if i have another problem.... ??? :blink:
__________________
Never mess with me when I have a cougar, Never! |
||
|
|
26-04-2005, 08:38 PM | #8 | ||
Join Date: Jun 2004
Location: Jan Mayen, Svalbard and Jan Mayen
Posts: 2,167
|
as long as you keep the source that demostrates the problem small and readable.
__________________
Flowing with the stream of life |
||
|
|
26-04-2005, 09:08 PM | #9 | ||
Join Date: Jan 2005
Location: ,
Posts: 454
|
Quote:
__________________
Never mess with me when I have a cougar, Never! |
||
|
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
looking for a nice simple but usefull joystick | Juanca | Tech Corner | 20 | 28-11-2008 12:17 AM |
Simple DOS games question..with out DOSBox? | Fubb | General compatibility fixes | 6 | 31-05-2008 12:09 AM |
looking for simple programming exercises? | Koen | Programming | 1 | 15-01-2008 11:52 PM |
Thread Tools | |
Display Modes | |
|
|
||
  |