class HasPtr
{
public:
HasPtr(const string &s = string()) : ps(new string(s)), i (0) {}
HasPtr(const HasPtr &hp) : ps(new string(*hp.ps)), i(hp.i) {}
HasPtr& operator=(const HasPtr &hp);
~HasPtr(){delete ps;};
private:
string *ps;
int i;
};
HasPtr& HasPtr::operator=(const HasPtr &hp)
{
auto newp = (new string(*hp.ps));
delete ps;
cout << *hp.ps << endl;;
ps = newp;
i = hp.i;
return *this;
}
I added the cout below "delete ps;" so I could see if hp.ps data still existed or deleted...but it still exists...so my question is...When I try:
HasPtr one("richard"), two;
two = one;
I'm rather confused about the whole concept so my question might seem confusing, but what the hell is happening with the delete??? What am I deleting? Am I deleting what ps points to? So shouldn't "cout << *hp.ps;" fail because *ps is pointing to that value??
Ran debugger a few times to try to understand what is happening. It seems that ps is apart of "*this" and if I dont use delete, than whatever string I place in "burger" will just be sitting in memory with no way to access it. I think I just answered my question thanks guys!

New Topic/Question
Reply



MultiQuote






|