简体   繁体   中英

Operator== to compare two different classes

So I have these two classes: Foo and Bar .

//forward declaration for Bar
class Bar;

typedef enum Type { TYPE1, TYPE2, TYPE3 };

// My class Foo
class Foo
{ 
public:
    explicit Foo(Type fooType) 
        :mType(fooType) 
    {} 

    Type getFooType() const
    {
        return mType;
    }

inline bool operator==(const Bar& bar) { return (bar.getBarType() == mType); } //ERROR: error C2027: use of undefined type 'Bar'

private:
    Type mType; 
};

// My class Bar
class Bar
{ 
public:
    explicit Bar(Type barType) 
        :mType(barType) 
    {} 

    Type getBarType() const
    {
        return mType;
    }

private:
    Type mType; 
};

Now somewhere in my code, I want to compare two instances (one from the Foo and other from Bar class). Like this:

if(myBar->getBarType() == myFoo->getFooType())
{
 //...
}

QUESTION: I know that I need to implement operator== to allow this comparation. So I've done as seen above... And I get that error, despite the fact that I've made the forward declaration. What am I missing here that allows me to compare using the operator== on both classes?

You need to define your operator== after the Bar class definition, rather than in the Foo class. Declare it in the class, but define it outside.

inline Foo::operator==(const Bar &bar) const { ... }

This won't quite help with your test above, since you have the Bar element on the left and the Foo on the right, so the operator== in Foo won't be considered. Defining symetric global operator== functions would be useful.

inline bool operator==(const Bar &bar, const Foo &foo) { return foo == bar; }

You say you want this comparison to work...

if(myBar->getBarType() == myFoo->getFooType())

That's comparing the enum Type values returned by the getXType functions, not the Foo and Bar objects themselves, and enums are comparable by default so you don't need to provide your own operator== .

Never-the-less, you have tried to do so, and in...

inline bool operator==(const Bar& bar) { return (bar.getBarType() == mType); } //ERROR: error C2027: use of undefined type 'Bar'

...the problem is that Bar is not defined at the place this function definition appears in the translation unit. You could just declare the function...

inline bool operator==(const Bar& bar);

...then define it later in the translation unit, after the definition of class Bar .

This would still only allow you to omit the explicit getFooType() call when comparing with a Foo as the left-hand-side argument and a Bar at right. To support the other ordering you asked for, you'd need a similar operator inside Bar that worked with a const Foo& .

One more detail... you say...

And I get that error, despite the fact that I've made the forward declaration.

A forward declaration just lets the compile know that Bar is a class, it doesn't tell the compiler that Bar contains a Type getBarType() member function, and that knowledge is needed to compile your operator== .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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