重载C+中的[]运算符+;支持自定义设置功能 我有一个C++类,它模拟一个数组,并操纵它的成员,我实现了两个函数:集合(siZez索引,sisitht值)< /> >和 GET(siZeZT索引)。我想重载[]运算符,使其具有以下功能: MyCustomArray[index] = value //->set(size_t index, size_t value)
及重载C+中的[]运算符+;支持自定义设置功能 我有一个C++类,它模拟一个数组,并操纵它的成员,我实现了两个函数:集合(siZez索引,sisitht值)< /> >和 GET(siZeZT索引)。我想重载[]运算符,使其具有以下功能: MyCustomArray[index] = value //->set(size_t index, size_t value),c++,operator-overloading,C++,Operator Overloading,及 get可以很容易地通过重载实现,但是我不知道如何实现set,因为我事先需要参数value 我的类是一个固定字数组的实现(数组中的元素最多有p位,其中p是一个参数,可以小于常规机器字)。为了支持此功能,set和get在常规C/C++数组中操作一个值的位范围 在这种情况下是否可能过载 提前谢谢 如注释中所述,这可以通过让操作符[]返回代理对象来实现,这就是std::vector发挥其魔力的方式 在你的例子中,它看起来是这样的: struct MyCustomArray; struct Arra
get
可以很容易地通过重载实现,但是我不知道如何实现set
,因为我事先需要参数value
我的类是一个固定字数组的实现(数组中的元素最多有p位,其中p是一个参数,可以小于常规机器字)。为了支持此功能,set
和get
在常规C/C++数组中操作一个值的位范围
在这种情况下是否可能过载
提前谢谢 如注释中所述,这可以通过让
操作符[]
返回代理对象来实现,这就是std::vector
发挥其魔力的方式
在你的例子中,它看起来是这样的:
struct MyCustomArray;
struct ArrayMemberRef {
MyCustomArray* target_;
std::size_t index_;
ArrayMemberRef& operator=(std::size_t value);
operator std::size_t();
};
struct MyCustomArray {
ArrayMemberRef operator[](std::size_t index) {
return ArrayMemberRef{this, index};
}
void set(std::size_t index, std::size_t value);
int get(std::size_t index);
};
ArrayMemberRef& ArrayMemberRef::operator=(std::size_t value) {
target_->set(index_, value);
return *this;
}
ArrayMemberRef::operator std::size_t() {
return target_->get(index_);
}
如注释中所述,这可以通过让
operator[]
返回代理对象来实现,这就是std::vector
执行其神奇功能的方式
在你的例子中,它看起来是这样的:
struct MyCustomArray;
struct ArrayMemberRef {
MyCustomArray* target_;
std::size_t index_;
ArrayMemberRef& operator=(std::size_t value);
operator std::size_t();
};
struct MyCustomArray {
ArrayMemberRef operator[](std::size_t index) {
return ArrayMemberRef{this, index};
}
void set(std::size_t index, std::size_t value);
int get(std::size_t index);
};
ArrayMemberRef& ArrayMemberRef::operator=(std::size_t value) {
target_->set(index_, value);
return *this;
}
ArrayMemberRef::operator std::size_t() {
return target_->get(index_);
}
这就像std::vector::operator[]所做的一样——使用代理对象
class MyCustomArray
{
public:
using value_type = unsigned;
class Proxy
{
public:
friend class MyCustomArray;
operator value_type() const
{
return m_customArray.get(m_index);
}
Proxy & operator=(value_type value)
{
m_customArray.set(m_index, value);
return *this;
}
private:
Proxy(MyCustomArray & customArray, size_t index)
: m_customArray(customArray), m_index(index) {}
MyCustomArray & m_customArray;
size_t m_index;
};
value_type operator[](size_t index) const
{
return get(index);
}
Proxy operator[](size_t index)
{
return Proxy(*this, index);
}
value_type get(size_t index) const;
void set(size_t index, value_type value);
private:
/// The data goes here
};
然后
无效工作(MyCustomArray&arr)
{
//返回一个代理对象,并在其上调用operator=。
arr[3]=5;
//arr_2的类型为MyCustomArray::Proxy
自动arr_2=arr[2];
arr_2=1;//修改arr[2]
无符号x=arr_2;//从arr[2]获取1
//由于自动转换为“值”类型,因此此操作有效:
std::cout这与std::vector::operator[]所做的一样-使用代理对象
class MyCustomArray
{
public:
using value_type = unsigned;
class Proxy
{
public:
friend class MyCustomArray;
operator value_type() const
{
return m_customArray.get(m_index);
}
Proxy & operator=(value_type value)
{
m_customArray.set(m_index, value);
return *this;
}
private:
Proxy(MyCustomArray & customArray, size_t index)
: m_customArray(customArray), m_index(index) {}
MyCustomArray & m_customArray;
size_t m_index;
};
value_type operator[](size_t index) const
{
return get(index);
}
Proxy operator[](size_t index)
{
return Proxy(*this, index);
}
value_type get(size_t index) const;
void set(size_t index, value_type value);
private:
/// The data goes here
};
然后
无效工作(MyCustomArray&arr)
{
//返回一个代理对象,并在其上调用operator=。
arr[3]=5;
//arr_2的类型为MyCustomArray::Proxy
自动arr_2=arr[2];
arr_2=1;//修改arr[2]
无符号x=arr_2;//从arr[2]获取1
//由于自动转换为“值”类型,因此此操作有效:
std::cout从std::vector
中汲取灵感,返回一个临时代理对象,该对象本身重载operator=
@Quentin应该有人慷慨地授予它自己的名字,而不是让它成为std::vector
中的一个破坏者,但它是实现所需功能的一个很好的例子。@PeteBecker最后一段中的一些琐事似乎阻止了这个简单的解决方案。@重复数据消除器唉!但是,是的,只是为了演示目的。@Quentin--说得好。我并没有读到最后-(已删除。从std::vector
中汲取灵感,返回一个自身过载的临时代理对象operator=
@Quentin应该有人慷慨地授予它自己的名字,而不是让它成为std::vector
破坏通用代码的一个疣子,但它是所需功能的一个很好的例子。@PeteBecker最后一段中的一点玩弄似乎阻止了这个简单的解决方案。@重复数据消除器唉!但是,是的,只是为了演示目的。@Quentin--说得好。我没有读到最后。:-(删除。谢谢,我使用了这种方法,它工作得很好!谢谢,我使用了这种方法,它工作得很好!