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.