I would like to wrap C types into a CPP class for better memory handling. For instance the below code snippet shows roughly what I would like to do:
class TJCompressor
{
public:
TJCompressor()
: m_tjInstance(tjInitCompress())
{
if (m_tjInstance == nullptr)
throw std::runtime_error("Could not create a TJ compressor instance");
}
~TJInstance()
{
tjDestroy(m_tjInstance);
}
const tjhandle& operator()() const
{
return m_tjInstance;
}
private:
tjhandle m_tjInstance = nullptr;
};
However, now I need to access the actual tjhandle through operator()()
and I would prefer to get rid of this.
TJCompressor compressor;
tjDecompressHeader3(decompressor(), ... ); // works as expected
tjDecompressHeader3(decompressor, ... ); // preferred way of doing it
I am pretty sure that this is achievable but I somehow can't find anything about how to do it.
What you want I think is a conversion operator .... something that looks like
operator const tjhandle & () const { return m_tjInstance; }
you will then be able to call your function as
tjDecompressHeader3(decompressor, ...)
More information can be found here: https://en.cppreference.com/w/cpp/language/cast_operator