最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin3浏览0评论

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);
}
发布评论

评论列表(0)

  1. 暂无评论