I have a class eg
class test{
public:
somedatahere
test();
~test();
private:
string mystring;
}
In this class the constructor reads the contents of a file in variable mystring.My question is :
Does mystring gets freed when the class destructs or I must free it manually? How can I free mystring ?
Since
mystringis part of the object, it will go out of scope when the object does. There’s no need to “manually” free it, and indeed you can’t.This would be different if
mystringwas a pointer to memory allocated withnew(ornew[]), then you’d have to manuallydelete(ordelete[]) it from your destructor.