i wrote a simple code but somehow it doesn't work.
can anyone please help me and tell me why does the content of the objects erase when it get to main.
thanks in advance.
#include <iostream>
#include <cassert>
#include <cstring>
using namespace std;
class String
{
char* pStr;
public:
String()
{
pStr = NULL;
}
String(char* str)
{
pStr = new char[strlen(str)+1];
assert(pStr!=0);
strcpy(pStr,str);
}
String &operator=(const String &ob)
{
if(&ob != this ) // check for self-assignment
{
if ( strlen(pStr) < strlen(ob.pStr) )
{ delete[] pStr;
pStr = new char [strlen(ob.pStr)+1];
assert(pStr != 0);
}
strcpy(pStr, ob.pStr);
}
when I tried compiling your code I got warnings that in the operator-, operator+ and operator* fcns you were trying to return a local variable?
what compiler are you using as a matter of curiousity and did it return warnings?
warnings indicate that your code can be compiled, but this is something that will probably not work or cause problems at runtime. they are there for a reason, use them.