C++按值和按引用比较运算符的多态性
我想比较从相同基类型派生但不是相同派生类的两个对象。因此,我将 == 运算符设为虚拟并在派生类中覆盖它。
当我将所有对象存储在基本类型的数组中时,不会调用派生实现,它直接进入基本实现。但是,当数组是指向基类的指针类型并且取消引用元素时,它确实可以工作。
有人可以解释一下为什么会出现这种行为吗?这让我很困惑;-)
enum eType {
BASE_TYPE,
DERIVED_TYPE
};
class A {
eType mType;
public:
A() : mType(BASE_TYPE) {}
A(eType Type) : mType(Type) {}
virtual bool operator == (A &Other) {
return mType == Other.mType;
}
};
class B : public A {
int bVal;
public:
B(int Val) : A(DERIVED_TYPE), bVal(Val) {}
virtual bool operator == (A &Other) {
if(!(A::operator ==(Other))) {
return false;
}
B* myB = (B*)&Other;
return bVal == myB->bVal;
}
};
int main(int argc, char *argv[])
{
A a1, a2;
B b1(0);
B b2(1);
bool result = false;
// Calls implementation in A
result = (a1 == a2);
// Calls implementation in B
result = (b1 == b2);
A aArray[2];
aArray[0] = b1;
aArray[1] = b2;
// Calls implementation in A!
result = (aArray[0] == aArray[1]);
A *aRefArray[2];
aRefArray[0] = &b1;
aRefArray[1] = &b2;
// Calls implementation in B
result = ((*aRefArray[0]) == (*aRefArray[1]));
return 0;
}
I want to compare two objects that are derived from the same base type but are not the same derived class. So I made the == operator virtual and over-ride it in derived classes.
When I store all of my objects in an array of the base type, the derived implementation is not called, it goes straight to the base implementation. It does however work when the array is of type pointer to base class, and a de-reference the elements.
Could some-one please explain why this behaviour occurs? It baffles me ;-)
enum eType {
BASE_TYPE,
DERIVED_TYPE
};
class A {
eType mType;
public:
A() : mType(BASE_TYPE) {}
A(eType Type) : mType(Type) {}
virtual bool operator == (A &Other) {
return mType == Other.mType;
}
};
class B : public A {
int bVal;
public:
B(int Val) : A(DERIVED_TYPE), bVal(Val) {}
virtual bool operator == (A &Other) {
if(!(A::operator ==(Other))) {
return false;
}
B* myB = (B*)&Other;
return bVal == myB->bVal;
}
};
int main(int argc, char *argv[])
{
A a1, a2;
B b1(0);
B b2(1);
bool result = false;
// Calls implementation in A
result = (a1 == a2);
// Calls implementation in B
result = (b1 == b2);
A aArray[2];
aArray[0] = b1;
aArray[1] = b2;
// Calls implementation in A!
result = (aArray[0] == aArray[1]);
A *aRefArray[2];
aRefArray[0] = &b1;
aRefArray[1] = &b2;
// Calls implementation in B
result = ((*aRefArray[0]) == (*aRefArray[1]));
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您这样做时,对象切片需要放置并且对象的派生部分刚刚被剥离。现在存储在数组中的对象现在仅表现为基类的对象。当然,操作符函数的基类版本会被调用。
适当地,保留派生类对象的
类型
,因为存储在数组中的只是指向实际对象的指针,而不是对象本身。由于保留了对象的类型,因此在这种情况下将调用运算符函数的派生类版本。
When you do that Object Slicing takes place and the derived part of the object just gets stripped off. The objects now stored inside the array now merely behave as objects of the Base class. And naturally, the Base class version of the operator function gets called.
Appropriately, preserves the
type
of the derived class objects, because what gets stored in the array is just pointer to the actual object not the object itself.Since the type of the object is preserved the derived class version of the operator functions gets called in this case.