In code I use I found a class declaration in a header file that contains a pointer to const as a member that is defined with 'new' right there in the header file.
A matching 'delete' is in the destructor (also defined right there in the header).
Is this usage ok? Does it create a memory leak? The destructor is called only when an object of that class is destroyed, either by a delete (when created with new) or by going out of scope (when created on the stack). But the 'new' is not in the constructor, but part of the class declaration. Isn't this executed earlier? Maybe even whenever the header is parsed? Is it guaranteed to me matched by the delete in the destructor?
class Foo {
public:
explicit Foo(){}
~Foo() {
delete this->bar;
}
private:
const Baz* bar = new Baz();
};