lielb
lielb

Reputation: 13

Protected destructor on an interface with release() method

So I'm working on an interface that has a factory method which is extern C and a release abstract method.

I want to prevent calling delete directly through a pointer to base but to use the release method instead.

The Interface:

class IMyInterface {
public:
    virtual void release() = 0;

protected:
    ~IMyInterface() = default;
};

Derived:

class MyConcreteImplementation final : public IMyInterface {
public:
    void release() override {
        delete this;
    }

protected:
    ~MyConcreteImplementation(){
        // Custom cleanup code //
    }
};

The factory method:

extern "C" IMyInterface* createMyInterface() {
    return new MyConcreteImplementation();
}

I was wondering about the following:

  1. Is protected, non-virtual, destructor the way to go here?
  2. I'm also planning on binding this API using pybind11 and its said in the docs that it's not possible to create bindings to a class with a non-public destructor (unless I use ::nodelete).

Thanks!

Upvotes: 1

Views: 86

Answers (0)

Related Questions