Forums

Forums (http://www.abandonia.com/vbullet/index.php)
-   Programming (http://www.abandonia.com/vbullet/forumdisplay.php?f=25)
-   -   Simple Problem (http://www.abandonia.com/vbullet/showthread.php?t=4701)

NrmMyth 26-04-2005 05:12 PM

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! :kosta:
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;
}

The exercise is about an array of characters, there are 2 copy constructors for allocating a new memory and then copying needed stuff, and a destructor for deleting that memory.
At the end of execution i get this problem:
http://img80.echo.cx/img80/5581/probi0eu.th.gif

I also know that the program worked fine before creating a destructor. :wall:

:cheers:

Data 26-04-2005 05:38 PM

the loops you use to copy a string are flawed.

for(i=strlen(p);i=0;i--)

Then it will be allright.

NrmMyth 26-04-2005 05:50 PM

Quote:

Originally posted by Data@Apr 26 2005, 07:38 PM
the loops you use to copy a string are flawed.

for(i=strlen(p);i=0;i--)

Then it will be allright.

Code:

for(i=strlen(p);i>=0;i--)
 *        *(pok+i)=*(p+i);

Cool... not cool no I fell like a dumbass... :cry:

Thanks Data! :ok: :kosta:

Now you have honour off closeing this topic.

... :wall:

Data 26-04-2005 06:25 PM

as you allready include cstring...
why not do a strcpy(pok,p);

Oh well That's outside the scope of this thread.

NrmMyth 26-04-2005 06:50 PM

Quote:

Originally posted by Data@Apr 26 2005, 08:25 PM
as you allready include cstring...
why not do a strcpy(pok,p);

Oh well That's outside the scope of this thread.

I could but isn't this way faster??? :blink:

Data 26-04-2005 07:00 PM

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.)

NrmMyth 26-04-2005 07:08 PM

Cool, great man....
...so can i PM you if i have another problem.... ???

:blink:

Data 26-04-2005 07:38 PM

as long as you keep the source that demostrates the problem small and readable.

NrmMyth 26-04-2005 08:08 PM

Quote:

Originally posted by Data@Apr 26 2005, 09:38 PM
as long as you keep the source that demostrates the problem small and readable.
NP, thanks :ok: :cheers:


The current time is 03:44 AM (GMT)

Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.