【问题标题】:How to overload a comma operator to assign values to an array如何重载逗号运算符以将值分配给数组
【发布时间】:2020-09-11 05:28:42
【问题描述】:

所以我有以下代码:

#include <map>
#include <iostream>
using namespace std;

template<class V, unsigned D>
class SparseArray
{
public:

    map<string,V> data;

    SparseArray(){}

    class Index
    {
    private:
        int dims[D]{};
    public:
        int& operator[](int index)
        {
            return dims[index];
        }

        const int& operator[](int index) const
        {
            return dims[index];
        }

        friend ostream& operator<<(ostream& os, const SparseArray<V,D>::Index& index)
        {
            os << '{';
            for(int i=0;i<D;i++)
            {
                os<<index.dims[i];
                if(i+1!=D)os<<',';
            }
            os << '}';
            return os;
        }
        Index operator,(Index index)
        {

        }

        Index(){for(int i=0;i<D;i++){dims[i]=0;}}
    };

};

int main()
{
SparseArray<int,3>::Index i;

i[0] = 1;
i[1] = 2;
i[2] = 7;

//i = 1,2,7; - that's what i'm trying to make work

cout<<i;
}

如何实现逗号运算符,以便i=1,2,7i[0] = 1; i[1] = 2; i[2] = 7; 执行完全相同的操作 到目前为止我所知道的是 i=1,2,7 相当于 i.operator=(1).operator,(2).operator,(7); ,我该如何使用它? 我从研究中知道重载逗号运算符是不寻常的,但我需要这样做,因为它符合项目的要求。

【问题讨论】:

  • 在您的情况下,最好使用初始化列表进行复制分配。然后你可以写i = {1, 2, 3};
  • 矩阵类的设计看起来很奇怪。为什么矩阵类本身没有operator[](和operator,)函数? Index 类的目的是什么?
  • 很遗憾,我对此没有发言权,作业必须完全按照这种风格进行i =1,2,7;
  • @Someprogrammerdude 这是一个稀疏数组项目的开始,许多功能还没有出现,现在我正在尝试让Index 类工作
  • @ArdentCoder 是的,i=1,2,7 应该为底层的i.dims 数组赋值

标签: c++ operator-overloading comma-operator


【解决方案1】:

如何实现逗号运算符,以便 obj = 1, 2, 7 执行 和obj.arr[0] = 1; obj.arr[1] = 2; obj.arr[2] = 7;做的一模一样?

这将彻底改变comma operator 的含义。我更喜欢初始化列表:

obj = {1, 2, 7};

关于此场景中逗号运算符的使用。

我从研究中知道重载逗号运算符是不寻常的,但我 需要按照项目的要求去做。

是的,我遇到过这样的老师。我认为他们只是想测试您是否可以在这些奇怪的约束下破解他们的任务。我的解决方案是基于您问题本身的隐藏线索。

到目前为止我所知道的是obj = 1, 2, 7 相当于 obj.operator=(1).operator,(2).operator,(7);

没错。请注意在此任务中 operator, 几乎是 operator= 的同义词:

obj.operator=(1).operator=(2).operator=(7);

所以,这只是实现这个技巧的问题:

Sample& Sample::operator,(const int& val)
{
    // simply reuse the assignment operator
    *this = val;

    // associativity of comma operator will take care of the rest
    return *this;
}

实施operator= 由您决定。

那你就可以了

obj = 1, 2, 7;

我编写了一个与您的示例类似的小型工作代码:Live Demo

编辑:

根据 Jarod 的评论,建议对这些运算符进行更合理的重载,您可以通过这种方式重载 operator=clear + push_back):

Sample& Sample::operator=(const int& val)
{
    arr[0] = val;
    length = 1;
    return *this;
}

operator,这样(push_back):

Sample& Sample::operator,(const int& val)
{
    // append the value to arr
    arr[length] = val;
    ++length;

    // associativity of comma operator will take care of the rest
    return *this;
}

把这个想法放在一起:Demo 2

【讨论】:

  • “注意在这个任务中操作符是 operator= 的同义词” OP 没有 operator=,对我来说 obj = 1, 2, 7; obj = 4, 4, 4; 1, 2, 7 被删除了第二次分配后。 obj = 1, 2, 7; 将更等同于 obj = {1, 2, 7};。所以分配是 clear+push_back 而, 只是 push_back。
猜你喜欢
  • 2016-08-22
  • 2011-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多