C++ nonmember binary operator with private inheritance - Stack Overflow

As has been pointed out elsewhere, the community thinks C++ binary operators like == should be nonmembe

As has been pointed out elsewhere, the community thinks C++ binary operators like == should be nonmember functions. This presents a problem for me with private inheritance.

#include <cassert>

class Parent
{
public:
    Parent(int arg = 0) {}
    int member() const { return member_; }
private:
    int member_;
};

bool operator== (const Parent& a, const Parent& b) { return a.member() == b.member(); }

class Child : private Parent
{
public:
    Child(int arg = 0) : Parent(arg) {}
};

bool operator== (const Child& a, const Child& b)
{
    return static_cast<const Parent&> (a) == static_cast<const Parent&> (b);
}

int main()
{
    assert(Child(1) == Child(1));
}

Having Child's operator== call Parent's is trivial if operator== is a member, but done this way, C++ complains that conversion from Child to Parent is inaccessible (as it should be).

Does the community have a fix, or do I do my own hack?

As has been pointed out elsewhere, the community thinks C++ binary operators like == should be nonmember functions. This presents a problem for me with private inheritance.

#include <cassert>

class Parent
{
public:
    Parent(int arg = 0) {}
    int member() const { return member_; }
private:
    int member_;
};

bool operator== (const Parent& a, const Parent& b) { return a.member() == b.member(); }

class Child : private Parent
{
public:
    Child(int arg = 0) : Parent(arg) {}
};

bool operator== (const Child& a, const Child& b)
{
    return static_cast<const Parent&> (a) == static_cast<const Parent&> (b);
}

int main()
{
    assert(Child(1) == Child(1));
}

Having Child's operator== call Parent's is trivial if operator== is a member, but done this way, C++ complains that conversion from Child to Parent is inaccessible (as it should be).

Does the community have a fix, or do I do my own hack?

Share asked Mar 7 at 16:37 Topological SortTopological Sort 2,9123 gold badges31 silver badges60 bronze badges 2
  • 2 friend default works in your case demo – Jarod42 Commented Mar 7 at 16:44
  • 2 Or just make it a member. The advantage of the non-member function is not a huge gain. But now I'm waxing subjective. If you have to friend the operator anyway... – sweenish Commented Mar 7 at 16:48
Add a comment  | 

2 Answers 2

Reset to default 3

This isnt specific to private inheritance, but you'd face the same when Child has any private members. You can declare the operator to be a friend when it needs private access:

class Child : private Parent  
{
public:
    friend bool operator==(const Child&,const Child&);
    Child(int arg = 0) : Parent(arg) {}
};

The most common way of doing this is to write a normally-named member function that implements the predicate; the binary operator just calls that function.

struct parent {

    bool equals(const parent& other) const {
        return _member == other._member;
    }

};

bool operator==(const parent& lhs, const parent& rhs) {
    return lhs.equals(rhs);
}

struct child : private parent {

    bool equals(const child& other) const {
        return parent::equals(other);
    }

};

bool operator==(const child& lhs, const child& rhs) {
    return lhs.equals(rhs);
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744917683a4600936.html

相关推荐

  • C++ nonmember binary operator with private inheritance - Stack Overflow

    As has been pointed out elsewhere, the community thinks C++ binary operators like == should be nonmembe

    1天前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信