So a task: we have a third party library, there is a class (call it Base). There is a hidden implementation provided by the library call it Impl. I need to write a Proxy. Unfortunately Base has a protected virtual function fn.
So the question is how much the code below is correct from C++ viewpoint? It currently works perfectly in Visual Studio and doesn't work in clang/gcc on Mac (but compiles without any warnings). I quite realize mechanisms which happen there, so if remove class Problem everything works on both platforms. I'd want to know if I should report a bug to clang or it's undefined/unspecified behavior of C++ standard.
Expected result of the code is to call Impl::fn() normally
class Base
{
protected:
virtual void fn(){}
};
class Impl : public Base
{
public:
Impl() : mZ(54){}
protected:
virtual void fn()
{
int a = 10; ++a;
}
int mZ;
};
class Problem
{
public:
virtual ~Problem(){}
int mA;
};
class Proxy : public Problem, public Base
{
public:
virtual void fn()
{
Base * impl = new Impl;
typedef void (Base::*fn_t)();
fn_t f = static_cast<fn_t>(&Proxy::fn);
(impl->*f)();
delete impl;
}
};
int main()
{
Proxy p;
p.fn();
}