thx salem,
Did operator "new" resembles to "realloc" functionality ?
say i initilization for pointer in constructor and free memory in destructor. still my code
gives run time error at destructor.
i have just switched from C to C++ programming.
Code:
---------------
test.h file
---------------------------
class demon
{
char* pName;
int nVal,nLen;
public:
demon():nVal(0),nLen(0)
{
pName = NULL;
}
demon operator= (const demon& d)
{
if(this != &d)
{
this->pName = new char(strlen(d.pName)+1);
memset(this->pName,0,strlen(d.pName)+1);
strcpy(pName,d.pName);
this->nVal = d.nVal;
}
return *this;
}
void set(int x,char* pstring)
{
nVal = x;
pName = (char*)malloc(strlen(pstring)+1);
memset(this->pName,0,strlen(pstring)+1);
strcpy(pName ,pstring);
}
void get()
{
cout<<nVal<<endl<<pName<<endl;
}
demon operator + (const demon& d1)//,const demon d2);
{
nLen =strlen(this->pName)+1;
nLen += strlen(d1.pName)+1;
this->pName = (char*)realloc(this->pName,nLen);
memset(this->pName,0,nLen);
strcat(this->pName, d1.pName);
return *this;
}
~demon()
{
delete(pName);
}
};
-----------------------
in test.cpp file
---------------------------
main()
{
demon obj_demon, obj_demon1, obj_demon2 ;
obj_demon.set(25,"mySample");
obj_demon2.set(45,"Overload demo");
obj_demon = obj_demon + obj_demon2;
obj_demon.get();
obj_demon2.get();
return 0;
}