mardi 29 novembre 2016

using-declaration for friend function

In C++11 it is possible to make a public member of a private base class accessible to the outside (public) with a using declaration. For example

class A {
private:
    int i = 2;
public:
    void f() { i = 3; }

    friend bool operator==(const A& l, const A& r) { return l.i == r.i; }
};

class B : private A {
public:
    using A::f;
};

int main() {
    B b, b2;
    b.f();
}

b.f() is possible because of the using A::f in the definition of B.

Is it possible write a similar declaration which would make the up-cast from B& to A& possible for the friend function operator==(A&, A&), so that b == b2 can be called in main()?

Aucun commentaire:

Enregistrer un commentaire