从子类重载== [英] Overloading == from a child class

查看:89
本文介绍了从子类重载==的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我有两节课:
一个具有一批数据成员的Vehicle 类.

然后,我的子/子类称为Car ,具有另一组数据成员.

我已经在Vehicle中重载了等于(==)运算符,以检查其公共数据成员集.然后,我在Car中重载等于(==),以检查其数据成员.我还需要一些方法来执行Car的Vehicle过载,因此我也可以检查常见成员.最好的方法是什么.

我正在做这样的事情:

Hello,

I have two classes:
a Vehicle class with a batch of data members.

Then I child/sub class called Car with another set of data members.

I have overloaded the equality (==) operator in Vehicle to check its set of common data members. Then I overload equality (==) in Car to check its data members. I also need some way to execute the Vehicle overload from Car''s so I can check the common members as well. What is the best way to do this.

I have it working doing something like this:

if (static_cast<const Vehicle>(*this) == static_cast<const Vehicle>(car))



这种方法的问题在于,自从尝试实例化Vehicle以来,它破坏了我返回并使Vehicle抽象的能力.

作为参考,下面是两个类的重载:
车辆(父/母):



The problem with this method is that it breaks my ability to go back and make Vehicle abstract since its trying to instantiate Vehicle.

For reference here are the overloads from the two classes:
Vehicle (parent/base):

bool Vehicle::operator ==(const Vehicle& vehicle) const
{
    bool listsMatch = true;
    if (passengerCount == vehicle.passengerCount)
    {
        for (int i = 0; i < passengerCount; ++i)
        {
            if (passengerList[i] != vehicle.passengerList[i])
            {
                listsMatch = false;
            }
        }
    }
    else
    {
        listsMatch = false;
    }
    if ((fuelCapacity == vehicle.fuelCapacity) &&
        (fuelRate == vehicle.fuelRate) &&
        (runningState == vehicle.runningState) &&
        (strcmp(name, vehicle.name) == 0) &&
        (listsMatch))
    {
        return true;
    }
    else
    {
        return false;
    }
}




和儿童/派生:




and child/derived:

bool Car::operator ==(const Car& car) const
{
    if (static_cast<const Vehicle>(*this) == static_cast<const Vehicle>(car))
    {
        if (inGear == car.inGear)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    else
    {
        return false;
    }
}



谢谢



Thanks

推荐答案

可以通过与普通方法相同的方式以及通过其运算符来调用运算符.因此,只需通过其方法调用它,然后指定您希望从下面看到它的父类:

Operators can be called in the same way as a normal method as well as through their operator. So simply invoke it through its method and specify which parent class you wish it to be called from see below:

bool Car::operator ==(const Car& car) const
{
    if (Vehicle::operator ==(car))
    {
...



应该可以解决您的问题.

Charles Keepax



Should fix your problem.

Charles Keepax


查尔斯"的答案是正确的.您收到错误消息的原因是在编写时:

Charles'' answer is the way to go. The reason you''re getting the error message is that when you write:

if (static_cast<const Vehicle>(*this) == static_cast<const Vehicle>(car))



您正在将几辆汽车的车辆复制到两个临时对象中.如果您通过引用将它们进行了比较,则不会发生:



you''re copying the vehicly bits of a couple of cars into two temporary objects. If you''d compared them by reference this wouldn''t have happened:

if( static_cast<const Vehicle&>(*this) == static_cast<const Vehicle&>(car))



话虽如此,但使用了Charles概述的语法,它更加直接和简洁.

另一个快速的风格问题是,您在派生类比较中进行了大量的ifs和elses运算.如果您还记得这样的表达式:



Having said that use the syntax Charles'' outlined, it''s a lot more direct and concise.

Another quick stylistic point is that you''re doing a lot of ifs and elses in the derived class comparison. If you remember that an expresion like:

inGear == car.inGear



具有布尔类型,值truefalse,您可以在一行中重写整个批次:



has a type of bool and a value of true or false you can rewrite the entire lot in one line:

bool Car::operator==( const Car &car ) const
{
    return Vehicle::operator==( car ) && inGear == car.inGear;
}



干杯,



Cheers,

Ash


这篇关于从子类重载==的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆