I searched for the answer to this question and wasn't able to find one. Consider the following code:
struct Foo
{
int *bar;
Foo(int barValue) : bar(new int(barValue)) {}
~Foo() { do_this(); }
void do_this() { delete bar; bar = nullptr; }
};
int main()
{
const Foo foo(7);
}
do_this()
cannot be called on a const
object, so I couldn't do something like foo.do_this()
. It would also make sense in some situations to call do_this()
outside of the destructor, which is why I don't want to simply include the code in the destructor definition. Because do_this()
modifies a member variable, I can't declare it as const
.
My question is: will the destructor be able to call do_this()
on a const
object when the object is destroyed?
I tried it and received no errors, but I want to make sure I'm not causing a memory leak once my program terminates.