Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何重载运算符以在运算符[]调用时调用setter函数?_C++_Operator Overloading_Getter Setter - Fatal编程技术网

C++ 如何重载运算符以在运算符[]调用时调用setter函数?

C++ 如何重载运算符以在运算符[]调用时调用setter函数?,c++,operator-overloading,getter-setter,C++,Operator Overloading,Getter Setter,如何重载类的运算符,以便使用 classInstance[index] = value; 表演 classInstance.cfgfile.Write(index,value) 背景资料;请随意跳过 我们开发的应用程序使用对NVRAM段的内存映射访问-实际上,映射的只是两个寄存器,地址和数据。写入地址寄存器,然后写入或读取数据寄存器。初始化后,读写操作由持有对内存段引用的类的简单[]重载执行。您引用实例的[],它给出了您要读写的单元的名称空间索引,它完成了它的工作 int& Ind

如何重载类的运算符,以便使用

classInstance[index] = value;
表演

classInstance.cfgfile.Write(index,value)

背景资料;请随意跳过

我们开发的应用程序使用对NVRAM段的内存映射访问-实际上,映射的只是两个寄存器,地址和数据。写入地址寄存器,然后写入或读取数据寄存器。初始化后,读写操作由持有对内存段引用的类的简单[]重载执行。您引用实例的[],它给出了您要读写的单元的名称空间索引,它完成了它的工作

int& IndirectMemory::operator[](RTCMemIndex idx)
{
    *midx_reg = idx;
    return *mdata_reg;
}
(代码去掉了互斥体和健全性检查等不相关的元素)

一切正常。。。只要NVRAM工作正常。这种特殊的芯片已经停产,而那些“野外”的芯片目前已经开始因年老而死亡。它们的功能对我们的使用意义不大,如果芯片损坏,我们可以将它们的角色转移到闪存上,而几乎没有影响(只是闪存磨损多一点)。问题是我们希望使用配置格式使用flash记录,它使用getter和setter

int TCfgFile::ReadKey(std::string Key);
void TCfgFile::WriteKey(std::string Key,int data);
在代码的许多地方,我们通过
IndirectMemory[Some_Register]=Some_Value调用NVRAM写入频繁更改的各种内容,我们希望通过重新启动保持这些内容。我希望保留此语法和行为,但如果通过配置条目检测到NVRAM已损坏或手动禁用,则能够写入文件


网络上充斥着使用操作符[]设置给定值的例子,只需返回对它的引用。用于:

在这种情况下,如果我调用,比如,
reg[3]=1
the
[]
将返回对元素#3的引用,默认的
运算符=
将很好地写入引用


但是,由于我无法返回对文件中键的引用(
.WriteKey()
只执行完整的写入,返回成功或错误),并且
操作符=
不带索引,因此,恐怕这个简单的选项不会有帮助。

您可以使用代理类来解决这个问题。由于
value
无法传递到
classInstance
中,因此我们需要创建一个对象,该对象可以由
操作符[]
返回,该对象将获得
value
的值,并知道该操作应用于哪个实例。使用

struct Proxy
{
    classInstance_type& to_apply;
    index_type index;
    Proxy(classInstance_type& to_apply, index_type index) : to_apply(to_apply), index(index) {}
    Proxy& operator=(value_type const & value)
    {
        to_apply.cfgfile.Write(index,value)
        return *this;
    }
};
您的类的
操作符[]
如下所示

Proxy operator[](index_type index)
{
    return Proxy{*this, index};
}

然后当你做
classInstance[index]=value
您可以调用
Proxy
操作符=
,该操作符包含要调用的对象的引用、要使用的索引以及您还需要的值。

您也可以在不使用代理类的情况下执行此操作。您可以使
操作符[]
返回对
*this
的引用,然后重载所述类的
=
操作符,以便对第二个参数中给定给
操作符=
的任何内容执行
写入

#include <iostream>

struct Foo {
    void Write(int idx, int value) {
        std::cout << "Write(" << idx << ", " << value << ")\n";
    }

    Foo& operator[](int idx) {
        this->index = idx;
        return *this;
    }
    void operator=(int value) {
        this->Write(this->index, value);
    }

    int index;
};

int main() {
    Foo f;
    f[5] = 10;
}
#包括
结构Foo{
无效写入(int idx,int值){

std::cout代理类。您可以使
classInstance[index]
返回一个带有重载
运算符=
的帮助器类的实例。该类应该捕获足够的信息,以便能够从其
运算符=(值)调用
classInstance.cfgfile.Write(index,value)
如果您想换一种方式-
value=classInstance[index];
-只需为代理提供一个从引用对象读取的
运算符value\u type()
转换运算符。
#include <iostream>

struct Foo {
    void Write(int idx, int value) {
        std::cout << "Write(" << idx << ", " << value << ")\n";
    }

    Foo& operator[](int idx) {
        this->index = idx;
        return *this;
    }
    void operator=(int value) {
        this->Write(this->index, value);
    }

    int index;
};

int main() {
    Foo f;
    f[5] = 10;
}