运算符==比较两个不同的类
Operator== to compare two different classes
所以我有这两个 classes:Foo
和 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;
};
现在在我的代码中的某处,我想比较两个实例(一个来自 Foo,另一个来自 Bar class)。
像这样:
if(myBar->getBarType() == myFoo->getFooType())
{
//...
}
问题:
我知道我需要实施 operator== 才能进行这种比较。
所以我已经做了上面看到的......
我得到了那个错误,尽管我已经做出了前向声明。
我在这里错过了什么让我可以在两个 classes 上使用 operator== 进行比较?
您需要在 Bar
class 定义之后定义您的 operator==
,而不是在 Foo
class 中。在 class 中声明它,但在外部定义它。
inline Foo::operator==(const Bar &bar) const { ... }
这对上面的测试没有多大帮助,因为左边有 Bar
元素,右边有 Foo
,所以 Foo 中的 operator==
赢了'被考虑。定义对称全局 operator==
函数会很有用。
inline bool operator==(const Bar &bar, const Foo &foo) { return foo == bar; }
您说您希望这种比较有效...
if(myBar->getBarType() == myFoo->getFooType())
这是比较 getXType
函数返回的 enum Type
值,而不是 Foo
和 Bar
对象本身,默认情况下枚举是可比较的,所以你不需要不需要提供您自己的 operator==
.
尽管如此,您已经尝试这样做,并且在...
inline bool operator==(const Bar& bar) { return (bar.getBarType() == mType); } //ERROR: error C2027: use of undefined type 'Bar'
...问题是 Bar
未定义在此函数定义出现在翻译单元中的位置。您可以只声明函数...
inline bool operator==(const Bar& bar);
...然后在翻译单元中定义它,在 class Bar
.
的定义之后
这仍然只允许您在比较左侧参数 Foo
和右侧参数 Bar
时省略显式 getFooType()
调用。为了支持您要求的其他排序,您需要在 Bar
中使用与 const Foo&
.
一起使用的类似运算符
再详细一点……你说……
And I get that error, despite the fact that I've made the forward declaration.
前向声明只是让编译器知道Bar
是一个class,它并没有告诉编译器Bar
包含一个Type getBarType()
成员函数,编译您的 operator==
.
需要这些知识
所以我有这两个 classes:Foo
和 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;
};
现在在我的代码中的某处,我想比较两个实例(一个来自 Foo,另一个来自 Bar class)。 像这样:
if(myBar->getBarType() == myFoo->getFooType())
{
//...
}
问题: 我知道我需要实施 operator== 才能进行这种比较。 所以我已经做了上面看到的...... 我得到了那个错误,尽管我已经做出了前向声明。 我在这里错过了什么让我可以在两个 classes 上使用 operator== 进行比较?
您需要在 Bar
class 定义之后定义您的 operator==
,而不是在 Foo
class 中。在 class 中声明它,但在外部定义它。
inline Foo::operator==(const Bar &bar) const { ... }
这对上面的测试没有多大帮助,因为左边有 Bar
元素,右边有 Foo
,所以 Foo 中的 operator==
赢了'被考虑。定义对称全局 operator==
函数会很有用。
inline bool operator==(const Bar &bar, const Foo &foo) { return foo == bar; }
您说您希望这种比较有效...
if(myBar->getBarType() == myFoo->getFooType())
这是比较 getXType
函数返回的 enum Type
值,而不是 Foo
和 Bar
对象本身,默认情况下枚举是可比较的,所以你不需要不需要提供您自己的 operator==
.
尽管如此,您已经尝试这样做,并且在...
inline bool operator==(const Bar& bar) { return (bar.getBarType() == mType); } //ERROR: error C2027: use of undefined type 'Bar'
...问题是 Bar
未定义在此函数定义出现在翻译单元中的位置。您可以只声明函数...
inline bool operator==(const Bar& bar);
...然后在翻译单元中定义它,在 class Bar
.
这仍然只允许您在比较左侧参数 Foo
和右侧参数 Bar
时省略显式 getFooType()
调用。为了支持您要求的其他排序,您需要在 Bar
中使用与 const Foo&
.
再详细一点……你说……
And I get that error, despite the fact that I've made the forward declaration.
前向声明只是让编译器知道Bar
是一个class,它并没有告诉编译器Bar
包含一个Type getBarType()
成员函数,编译您的 operator==
.