0
class Base
{
};

class Deriv : private Base
{
public:
    operator Base & ()
    {
        return *this;
    }
};

So I got this simple code. I'd like to make the implicit base conversion operator accessible in the deriv class. The solution above doesn't work because the compiler will always use the default operation (slicing) (see C++ Define Conversion to Base Class) BUT in my case I just want to make it accessible, not overloaded. Something like

public:
    using operator Base &();

it seems not possible, but I wonder why and if there are other solutions (aside of making an explicit function).

Please don't ask me why I'm doing this, I'm just looking if there's a solution to this issue I found for a very specific scenario.

QbProg
  • 1,675
  • 1
  • 14
  • 18
  • 5
    If you want to make `Deriv&` to be convertible to `Base&`, why are using `private` inheritance at all? – R Sahu May 03 '18 at 19:47
  • ^ Did you truly expect us not to ask? :P – HolyBlackCat May 03 '18 at 19:48
  • You should definitely bring some light on the nature of that *"very specific scenario"*. – user7860670 May 03 '18 at 19:58
  • In my case Base is an interface which I want to be accessible only by some specific functions taking "Base &". Regular class usage is not allowed though the Base functions. Of course I can just make a casting function, but for compatibility reasons I had thousands function calls using the implicit cast. I ended up making a class-in-the-middle where I protected all Base members with "using" and derived from it. Still, it was a strange language corner case – QbProg May 04 '18 at 06:29

0 Answers0