C++ 比较来自两个不同类的对象?

C++ 比较来自两个不同类的对象?,c++,inheritance,C++,Inheritance,你好,我一直在努力想办法。我在这个函数中传递一个对象来比较两个不同的卷,但是我需要使用两个不同的类(继承)来接收卷的信息。当我试图传递对象时,我很难同时使用circleType和cylinderType类来传递对象。我不知道如何正确地处理这个问题,但我正试图找出如何在比较函数中使用两个不同的类 bool equalVolume(const circleType&, const cylinderType& ct) { if (ct.CalcVolume != Calvolu

你好,我一直在努力想办法。我在这个函数中传递一个对象来比较两个不同的卷,但是我需要使用两个不同的类(继承)来接收卷的信息。当我试图传递对象时,我很难同时使用circleType和cylinderType类来传递对象。我不知道如何正确地处理这个问题,但我正试图找出如何在比较函数中使用两个不同的类

bool equalVolume(const circleType&, const cylinderType& ct)
{
    if (ct.CalcVolume != Calvolume())
    {
        return false;
    }
    else
    {
        return true;
    }
}

您的实现存在多个问题。您将收到两个形状,但
圆圈类型
没有变体名称。然后我看到您尝试调用函数
calcVolume
,但没有变量。你怎么能计算零的体积?嗯,你不能。您必须使用圆的名称并引用它

//                                 v---- Give a name to your circle
bool equalVolume(const CircleType& myCircle, const CylinderType& myCylinder) {
//      v---- Call calcVolume with myCircle
    if (myCircle.calcVolume() != myCylinder.calcVolume()) {
//      call with the cylinder --^
        return false
    } else {
        return true;
    }
}
顺便说一下,由于比较已经是一个类型为
bool
的表达式,您可以将函数缩减为:

bool equalVolume(const CircleType& myCircle, const CylinderType& myCylinder) {
    return myCircle.calcVolume() == myCylinder.calcVolume();
}

我更愿意创建一个函数,返回卷并将实现留给用户。看起来是这样的:

using volume_t = double;

struct Circle {
    volume_t volume() const {
        // return volume here
    }
};

struct Cylinder {
    volume_t volume() const {
        // return volume here
    }
};
template<typename S1, typename S2>
auto volumeEqual(const S1& s1, const S2& s2) -> decltype(s1.volume() == s2.volume()) {
    return s1.volume() == s2.volume();
}
然后,不要使用
isVolumeEqual
函数,只需执行以下操作:

if (myCircle.volume() == myCylinder.volume()) {
    // code here ...
}
如果您真的想实现volume equal函数,我会这样做:

using volume_t = double;

struct Circle {
    volume_t volume() const {
        // return volume here
    }
};

struct Cylinder {
    volume_t volume() const {
        // return volume here
    }
};
template<typename S1, typename S2>
auto volumeEqual(const S1& s1, const S2& s2) -> decltype(s1.volume() == s2.volume()) {
    return s1.volume() == s2.volume();
}
模板
自动卷相等(常数S1和S1,常数S2和S2)->decltype(S1.volume()==S2.volume()){
返回s1.volume()==s2.volume();
}

这样,您就可以为具有
volume()
功能的所有可能形状实现
volumeEqual

您的实现存在多个问题。您将收到两个形状,但
圆圈类型
没有变体名称。然后我看到您尝试调用函数
calcVolume
,但没有变量。你怎么能计算零的体积?嗯,你不能。您必须使用圆的名称并引用它

bool equalVolume(const circleType&, const cylinderType& ct)
{
    if (ct.CalcVolume != Calvolume())
    {
        return false;
    }
    else
    {
        return true;
    }
}
//                                 v---- Give a name to your circle
bool equalVolume(const CircleType& myCircle, const CylinderType& myCylinder) {
//      v---- Call calcVolume with myCircle
    if (myCircle.calcVolume() != myCylinder.calcVolume()) {
//      call with the cylinder --^
        return false
    } else {
        return true;
    }
}
顺便说一下,由于比较已经是一个类型为
bool
的表达式,您可以将函数缩减为:

bool equalVolume(const CircleType& myCircle, const CylinderType& myCylinder) {
    return myCircle.calcVolume() == myCylinder.calcVolume();
}

我更愿意创建一个函数,返回卷并将实现留给用户。看起来是这样的:

using volume_t = double;

struct Circle {
    volume_t volume() const {
        // return volume here
    }
};

struct Cylinder {
    volume_t volume() const {
        // return volume here
    }
};
template<typename S1, typename S2>
auto volumeEqual(const S1& s1, const S2& s2) -> decltype(s1.volume() == s2.volume()) {
    return s1.volume() == s2.volume();
}
然后,不要使用
isVolumeEqual
函数,只需执行以下操作:

if (myCircle.volume() == myCylinder.volume()) {
    // code here ...
}
如果您真的想实现volume equal函数,我会这样做:

using volume_t = double;

struct Circle {
    volume_t volume() const {
        // return volume here
    }
};

struct Cylinder {
    volume_t volume() const {
        // return volume here
    }
};
template<typename S1, typename S2>
auto volumeEqual(const S1& s1, const S2& s2) -> decltype(s1.volume() == s2.volume()) {
    return s1.volume() == s2.volume();
}
模板
自动卷相等(常数S1和S1,常数S2和S2)->decltype(S1.volume()==S2.volume()){
返回s1.volume()==s2.volume();
}

这样,您可以为所有可能的具有
volume()
函数的形状实现
volumeEqual

我认为它可能是这样的:

bool equalVolume(const circleType&, const cylinderType& ct)
{
    if (ct.CalcVolume != Calvolume())
    {
        return false;
    }
    else
    {
        return true;
    }
}
bool equalVolume(const circleType& cir, const cylinderType& cyl)
{
    return cir.CalcVolume == cyl.CalcVolume;
}
正如另一位用户指出的,我可能会添加一个volume()getter函数并使用它,而不是直接访问CalcVolume(它可能是一个数据成员)


PS:编写一个函数来比较这样的卷看起来是多余的,因为您总是可以只比较if()条件中的卷。

我认为它可能是这样的:

bool equalVolume(const circleType& cir, const cylinderType& cyl)
{
    return cir.CalcVolume == cyl.CalcVolume;
}
正如另一位用户指出的,我可能会添加一个volume()getter函数并使用它,而不是直接访问CalcVolume(它可能是一个数据成员)


PS:编写一个函数来比较这样的卷看起来是多余的,因为您总是可以在if()条件下比较卷。

您先做了一些错误更正,如果
circelType
cylinderType
都有
calcVolume
函数,那么它就可以工作了。您可能指的是
return(cleft.calcVolume()==cright.calcVolume())
作为函数体
bool equalVolume(const circleType&cleft,const cylinderType&cright)
您还可以重新设计应用程序,以便
class circleType
class cylinderType
继承自一个公共超类
GeometricalObject
声明一些
virtual double CalcVolume(void)const;
方法。然后你只需要一个
bool sameValue(const GeometricalObject&left,GeometricalObject&right);
函数你先做了一些拼写错误更正,然后如果
circelType
cylinderType
都有
calcVolume
函数你可能指的是
返回(cleft.calcVolume())==cright.CalcVolume();
作为函数体
bool equalVolume(const circleType&cleft,const cylinderType&cright)
您还可以重新设计应用程序,使
类CircletType
类cylinderType
都继承自一个公共超类
几何对象
声明一些
虚拟双CalcVolume(void)const;
方法。然后您只需要一个
bool sameValue(const GeometricalObject&left,GeometricalObject&right);
function函数是一个好主意,它可以帮助提高代码可读性,并可用于算法。a函数是一个好主意,它可以帮助提高代码可读性,并可用于算法