简体   繁体   English

朋友功能的使用声明

[英]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. 在C ++ 11中,可以using声明使外部(公共)可以访问私有基类的公共成员。 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();
}

bf() is possible because of the using A::f in the definition of B . bf()是可能的,因为在B的定义中using A::f

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() ? 是否有可能编写一个类似的声明,它可以使B& A&向上转换为A&可能为友元函数operator==(A&, A&) ,以便可以在main()调用b == b2

No, only B can internally cast itself to A , and it otherwise is not possible because from a client's perspective B is not an A but rather has an A 没有,只有B可以在内部投自己A ,它要不就是不可能的,因为从客户的角度来看, B 不是 A ,而是 A

Even if you replaced your friend bool operator= with a member function equals : 即使你用你的friend bool operator=替换成员函数equals

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

    bool equals(const A& r){return i == r.i;}
};

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

While this compiles, you cannot ever call b.equals(b2) because no implicit conversion is ever possible from a type of B to a type of A from the caller's perspective (due to private inheritance) . 在编译时,你不能调用b.equals(b2)因为从调用者的角度来看,从B类型到A类型都不可能进行隐式转换(由于私有继承)。

You'll need to provide your own operator== or change your inheritance to public or protected . 您需要提供自己的operator==或将您的继承更改为publicprotected Here's an example where B declares its own friend bool operator== 这是一个例子,其中B声明了自己的friend bool operator==

class B : private A {
public:
    using A::f;
    friend bool operator==(const B& l, const B& r)
    {
        return (static_cast<A>(l) == static_cast<A>(r)) && true; 
        // "true" is a stand-in for some other condition
    }
};

Read more at isocpp isocpp阅读更多内容


Edit: If you really want to play games, you will notice that I said no implicit conversion is ever possible, but some explicit conversions are. 编辑:如果你真的想玩游戏,你会注意到我说没有隐含的转换是可能的,但是一些明确的转换是。 Because B does technically derive from A you can do pointer casting to make it work, but I don't recommend it: 因为B在技​​术上从A派生,你可以做指针转换使其工作,但我不推荐它:

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

    bool equals(const A* r){return i == r->i;}
};

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

int main() {
    B b, b2;
    b.f();
    (::A*)(&b)->equals((::A*)(&b2));  
}

Or you could use pointer casting's ugly cousin, reference casting, if you wish to keep the original operator== syntax 或者,如果您希望保留原始operator==语法,则可以使用指针转换的丑陋表兄,引用转换

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();
    ((::A&)(b)) == ((::A&)(b2));  
}

See §11.2 [class.access.base] for more 有关更多信息,请参见§11.2[class.access.base]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM