Gabriel
Gabriel

Reputation: 97

How to overload an operator == inside an other class

I have a class A

In a template class B

template<class key>
class B

I want to overload the ==operator of A inside the class B, because I don't want it to be overloaded outside the class

how can I do that?

I tried:

1.

bool operator==(const key &a, const key &b)

compilation result: too many arguments

2.

friend operator==(const key &a, const key &b)

when I tried to use the operator, compilation result: can't find operator

Upvotes: 1

Views: 61

Answers (1)

YSC
YSC

Reputation: 40060

You can define a nested private wrapper for your key type:

template<class key>
class B
{
    struct EKey {
        key k;
        friend bool operator==(const EKey&, const EKey&) { return false; }
    };
    // ...
};

Full demo: http://coliru.stacked-crooked.com/a/2fd8e570f2b12a3e

Upvotes: 3

Related Questions