Click here to Skip to main content
15,867,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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))


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):
C#
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:

C#
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
Posted

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))
    {
...


Should fix your problem.

Charles Keepax
 
Share this answer
 
v2
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))


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


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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900