C++ 如何重载逗号运算符为数组赋值
所以我得到了以下代码:C++ 如何重载逗号运算符为数组赋值,c++,operator-overloading,comma-operator,C++,Operator Overloading,Comma Operator,所以我得到了以下代码: #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[
#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;
}
#包括
#包括
使用名称空间std;
模板
类散斑鱼
{
公众:
地图数据;
SparseArray(){}
类索引
{
私人:
int-dims[D]{};
公众:
整型运算符[](整型索引)
{
返回dims[索引];
}
常量int&运算符[](int索引)常量
{
返回dims[索引];
}
friend ostream&运营商
如何实现逗号运算符,以便obj=1,2,7
执行以下操作
与执行以下操作完全相同:obj.arr[0]=1;obj.arr[1]=2;obj.arr[2]=7;
这将完全改变的含义。我更喜欢初始值设定项列表:
obj = {1, 2, 7};
在这个场景中,逗号操作符的用法
从研究中我知道重载逗号运算符是不寻常的,但我
需要按照项目的要求来做
是的,我见过这样的老师。我想他们只是想测试你是否能在这些奇怪的约束条件下完成他们的任务。我的解决方案是基于你问题本身隐藏的线索
到目前为止,我所知道的是,obj=1,2,7
相当于
obj.operator=(1).operator,(2).operator,(7);
没错。请注意,在本任务中,
运算符几乎与运算符=
同义:
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;
我制作了一个小的工作代码,类似于您的示例:
编辑:
根据Jarod的意见,建议对这些运算符进行更合理的重载,您可以通过这种方式(清除+推回)重载operator=
:
和操作员,
以这种方式(向后推):
把这个想法放在一起:
如何实现逗号运算符,以便obj=1,2,7
执行以下操作
与执行以下操作完全相同:obj.arr[0]=1;obj.arr[1]=2;obj.arr[2]=7;
这将完全改变的含义。我更喜欢初始值设定项列表:
obj = {1, 2, 7};
在这个场景中,逗号操作符的用法
从研究中我知道重载逗号运算符是不寻常的,但我
需要按照项目的要求来做
是的,我见过这样的老师。我想他们只是想测试你是否能在这些奇怪的约束条件下完成他们的任务。我的解决方案是基于你问题本身隐藏的线索
到目前为止,我所知道的是,obj=1,2,7
相当于
obj.operator=(1).operator,(2).operator,(7);
没错。请注意,在本任务中,运算符几乎与运算符=
同义:
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;
我制作了一个小的工作代码,类似于您的示例:
编辑:
根据Jarod的意见,建议对这些运算符进行更合理的重载,您可以通过这种方式(清除+推回)重载operator=
:
和操作员,
以这种方式(向后推):
把这个想法放在一起:在您的情况下,最好使用初始值设定项列表进行副本赋值。然后您可以编写
i={1,2,3};
矩阵类的设计似乎很奇怪。为什么不使用操作符[]
(和操作符,
)矩阵类本身的函数?索引
类的目的是什么?遗憾的是,我在这方面没有发言权,赋值必须以这种方式执行i=1,2,7;
@Someprogrammerdude这是稀疏数组项目的乞讨,许多特性还没有出现,现在我正试图在索引classwork@ArdentCoder是的,i=1,2,7
应该给底层的i.dims
数组赋值。在您的情况下,最好使用初始值设定项列表进行副本赋值。然后您可以编写i={1,2,3};
矩阵类的设计似乎很奇怪。为什么不使用操作符[]
(和操作员,
)矩阵类本身的函数?索引
类的目的是什么?遗憾的是,我在这方面没有发言权,赋值必须以这种方式执行i=1,2,7;
@Someprogrammerdude这是稀疏数组项目的乞讨,许多特性还没有出现,现在我正试图在索引classwork@ArdentCoder是的,i=1,2,7
应该为底层的i.dims
数组赋值“注意操作符,在这个任务中是操作符=的同义词”OP没有操作符=
,对我来说,在obj=1,2,7;obj=4,4,4;
中1,2,7
在第二次赋值后被删除。obj=1,2,7;
将更等同于obj={1,2,7};
。所以赋值是清除+推回,而,
只是推回。“注意操作符,在这个任务中是操作符=的同义词”OP没有operator=
,对我来说在obj=1,2,7;obj=4,4,4;
1,2,7
在第二次赋值后被删除。obj=1,2,7;
将更等同于obj={1,2,7}
。所以赋值是清除+向后推,而,
只是向后推。