I have two classes, one is privately derived from the other (because I don't want to expose the interface of the base). However later I want to create a reference to the base.
I can do it with a normal member function base()
but not with a cast operator because this operator is never called
clang warns about it, because it says "it will never be called".
Why is it that cast operator is ignored and overwritten by a private element? Is this an inconsistency in the language?
Actually, I think it has a it makes public the base reference and nothing else.
Even more, if it worked it could be explicit
.
class A{
int v_;
public:
void f(){}
};
class B : A{ // A is private base because I don't want the f() interface
int w_;
public:
A const& base() const{return *this;}
/*explicit*/ operator A const&() const{return *this;} // never called, warning in clang
};
int main(){
A a = {};
B b = {};
A const& a2 = b.base();
A const& a3 = b; // bad, But why?
A const& a4{b}; // explict doesn't help
A const& a5 = b.operator A const&(); // works in clang (but with a contradictory warning), doesn't work with gcc
}