【问题标题】:Which is better: overloading operator* or overloading cast operator?哪个更好:重载 operator* 或重载强制转换运算符?
【发布时间】:2016-03-09 22:00:46
【问题描述】:

我有两个类,Permutation 和 Cycle。循环本质上是一种特殊的排列,但它不是 Permutation 的派生类,部分原因如下。两个排列的乘积是另一个排列,而循环的乘积通常不是另一个循环。一个循环和一个排列的乘积就是一个排列。

对于排列 p1、p2 和循环 c1、c2,我希望以下语句的预期含义应该清楚:

Permutation p3 = p1*p2;
Permutation p4 = p1*c1;
Permutation p5 = c1*p1; 
Permutation p6 = c1*c2;

我的 Cycle 和 Permutation 类的基本结构如下:

class Cycle
{
public:
   // stuff ...     
};

class Permutation
{
public:
    Permutation(const Cycle& cycle);

    friend
    Permutation operator*(const Permutation& left, const Permutation& right);
    // stuff ...
};

我如何/应该如何添加代码(或更改代码)以实现上述预期用途?

我有一些(可能有缺陷的)想法:

修改Permutation类的第一个思路是这样的:

class Permutation
{
    friend Permutation operator*(const Permutation&, const Cycle&);
    friend Permutation operator*(const Cycle&, const Permutation&);
    friend Permutation operator*(const Cycle&, const Cycle&);

public:
    Permutation(const Cycle& cycle);

    friend
    Permutation operator*(const Permutation& left, const Permutation& right);
    // stuff ...
};

我认为这可行,但我认为这可能是提供转换运算符的好情况。

这是我的第二个想法:

class Permutation
{

public:
    Permutation(const Cycle& cycle);

    friend
    Permutation operator*(const Permutation& left, const Permutation& right);
    // stuff ...
};

class Cycle
{
public:
    operator Permutation() const
    {
        Permutation p(*this);
        return p;
    }
    // stuff ...     
};

我认为第二个想法应该适用于:

Permutation p4 = p1*c1;

但不是:

Permutation p5 = c1*p1; 
Permutation p6 = c1*c2;

这是第三个想法:

class Permutation
{
    friend Permutation operator*(const Permutation&, const Permutation&);

public:
    Permutation(const Cycle& cycle);

    //friend
    //Permutation operator*(const Permutation& left, const Permutation& right);
    // stuff ...
};

class Cycle
{
public:
    operator Permutation() const
    {
        Permutation p(*this);
        return p;
    }
    // stuff ...     
};

我怀疑这可能适用于

Permutation p4 = p1*c1;
Permutation p5 = c1*p1; 

但我无法想象编译器会知道在赋值中对两个参数进行类型转换:

Permutation p6 = c1*c2;

提前感谢您的 cmets 和建议。

【问题讨论】:

  • 关于“我如何/应该如何添加代码(或更改代码)以实现上述预期用途?”,您为什么认为您必须额外做任何事情 ?无论如何,当您尝试传达您的真实代码是什么时,请发布真实代码。我修复了缺少的 friend 关键字,但可能还有其他问题可能会造成不必要的混淆。
  • 投票结束,因为缺乏可重复的例子。

标签: c++ type-conversion operator-overloading


【解决方案1】:

我认为你的错误不是从 Permutation 派生的循环。这是我的建议:

class Permutation
{
public:
    virtual bool IsCycle()
    {
        return false;
    }

    virtual Permutation& operator*(Permutation& right);
};

class Cycle: public Permutation
{
public:
    virtual bool IsCycle()
    {
        return true;
    }

    Permutation& operator*(Permutation& right);
};

Permutation& Permutation::operator*(Permutation& right)
{
    // in this case left operand is NOT a Cycle
    // right operand could be anything

    Permutation& p1 = *this;

    if(right.IsCycle())
    {
        Cycle& c1 = *dynamic_cast<Cycle*>(&right);
        // code for case Permutation p4 = p1*c1;
    }
    else
    {
        Permutation &p2 = right;
        // code for Permutation p3 = p1*p2;
    }
}

Permutation& Cycle::operator*(Permutation& right)
{
    // in this case left operand IS a Cycle
    // right operand could be anything
    Cycle& p1 = *this;
    if(right.IsCycle())
    {
        Cycle& c2 = *dynamic_cast<Cycle*>(&right);
        // code for case Permutation p6 = c1*c2;
    }
    else
    {
        Permutation &p1 = right;
        // code for Permutation p5 = c1*p1; 
    }
}

【讨论】:

    猜你喜欢
    • 2012-11-06
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 2014-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-27
    相关资源
    最近更新 更多