My code is below. Initially it showed an error that a same memory location is being freed twice.i.e Returned object temp is freed twice(while return and after copied to obj2). So, i overloaded the copy constructor to have different memory while coping. Then error disappeared but a garbage value was stored in obj2 instead of "ll". Then I overloaded the = operator void main line 2 from shallow to deep copy. Now garbage value is gone but it has a null value instead of "ll".I don't want to comment free() or use any functions from #include.Can someone say what should do?.
class CustomStringClass
{
private:
char* Input;
public:
CustomStringClass (string Input){
//Dynamic mem allocation done to char*Input and store the input
}
CustomStringClass Substring(int Start, int End){
//Substring found as "ll" from "Hello"
CustomStringClass temp("ll");
return temp;
}
~CustomStringClass(){
free(this->Input);
}
};
void main()
{
CustomStringClass Obj1("Hello");
CustomStringClass Obj2=Obj1.Substirng(2,3);
}