Go Back   Forums > Community Chatterbox > Tech Corner > Programming
Memberlist Forum Rules Search Today's Posts Mark Forums Read
Search Forums:
Click here to use Advanced Search

Reply
 
Thread Tools Display Modes
Old 26-04-2005, 06:12 PM   #1
NrmMyth
Hero Gamer

 
Join Date: Jan 2005
Location: ,
Posts: 454
Red face

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


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

__________________
Never mess with me when I have a cougar, Never!
NrmMyth is offline                         Send a private message to NrmMyth
Reply With Quote
Old 26-04-2005, 06:38 PM   #2
Data
retired
 
Data's Avatar


 
Join Date: Jun 2004
Location: Jan Mayen, Svalbard and Jan Mayen
Posts: 2,167
Default

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
Data is offline                         Send a private message to Data
Reply With Quote
Old 26-04-2005, 06:50 PM   #3
NrmMyth
Hero Gamer

 
Join Date: Jan 2005
Location: ,
Posts: 454
Default

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

Thanks Data! k:

Now you have honour off closeing this topic.

...
__________________
Never mess with me when I have a cougar, Never!
NrmMyth is offline                         Send a private message to NrmMyth
Reply With Quote
Old 26-04-2005, 07:25 PM   #4
Data
retired
 
Data's Avatar


 
Join Date: Jun 2004
Location: Jan Mayen, Svalbard and Jan Mayen
Posts: 2,167
Default

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
Data is offline                         Send a private message to Data
Reply With Quote
Old 26-04-2005, 07:50 PM   #5
NrmMyth
Hero Gamer

 
Join Date: Jan 2005
Location: ,
Posts: 454
Default

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:
__________________
Never mess with me when I have a cougar, Never!
NrmMyth is offline                         Send a private message to NrmMyth
Reply With Quote
Old 26-04-2005, 08:00 PM   #6
Data
retired
 
Data's Avatar


 
Join Date: Jun 2004
Location: Jan Mayen, Svalbard and Jan Mayen
Posts: 2,167
Default

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
Data is offline                         Send a private message to Data
Reply With Quote
Old 26-04-2005, 08:08 PM   #7
NrmMyth
Hero Gamer

 
Join Date: Jan 2005
Location: ,
Posts: 454
Default

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

:blink:
__________________
Never mess with me when I have a cougar, Never!
NrmMyth is offline                         Send a private message to NrmMyth
Reply With Quote
Old 26-04-2005, 08:38 PM   #8
Data
retired
 
Data's Avatar


 
Join Date: Jun 2004
Location: Jan Mayen, Svalbard and Jan Mayen
Posts: 2,167
Default

as long as you keep the source that demostrates the problem small and readable.
__________________
Flowing with the stream of life
Data is offline                         Send a private message to Data
Reply With Quote
Old 26-04-2005, 09:08 PM   #9
NrmMyth
Hero Gamer

 
Join Date: Jan 2005
Location: ,
Posts: 454
Default

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 k:
__________________
Never mess with me when I have a cougar, Never!
NrmMyth is offline                         Send a private message to NrmMyth
Reply With Quote
Reply


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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump
 


The current time is 02:35 PM (GMT)

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