运算符重载 C++:只写版本 [英] Operator Overloading C++: write-only version

查看:75
本文介绍了运算符重载 C++:只写版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为数据结构重载运算符,所以我有标准的函数声明:

I am overloading operators for a data structure, so I have the standard function declarations:

T & operator[](int i);    //used for regular objects
const T & operator[](int i) const;  // used for const objects

所以我想要做的是为常规对象提供两个版本的 operator[]:当 operator[] 用于写入而不是读取时,它会做一些不同的事情.

So what I want to do is to have a two versions of operator[] for regular objects: one that does something different when the operator[] is used to write rather than read.

我一直在读到这是可能的,但我还没有看到任何代码.

I have been reading that this is possible, but I have not yet seen any code.

这个问题我看过很多次了,我也看过答案'operator[] const' version is used for reading"--> 但事实并非如此;它仅用于类的 const 实例化.

I have seen many times this question asked, and I have seen the answer " 'operator[] const' version is used for reading" --> but this is not true; it is used only with const instantiations of the class.

任何人都可以提供有关检测写入事件以触发不同行为的指导吗?也许是复制构造函数中的技巧?

Can anyone offer guidance on detecting the write event to trigger different behavior? Is perhaps the trick in the copy constructor?

推荐答案

持有对象的类无法获取您对返回对象的访问权限是读访问还是写访问的信息.

The class holding the objects cannot get the information whether your access to the returned object is read or write access.

只有对象本身通过成员函数限定符有一些我在哪个上下文中使用"的概念.

Only the object itself has some notion of "in which context am I used" via the member function qualifiers.

  • 引用限定符
  • const/volatile 限定符

您可以在代理类中使用它.

You can use this in a proxy class.

#include <vector>
#include <type_traits>
#include <iostream>

template <class T, class U = T, bool Constant = std::is_const<T>::value>
class myproxy
{
protected:
  U& m_val;
  myproxy& operator=(myproxy const&) = delete;
public:
  myproxy(U & value) : m_val(value) { }
  operator T & ()
  {
    std::cout << "Reading." << std::endl;
    return m_val;
  }
};

template <class T>
struct myproxy < T, T, false > : public myproxy<T const, T>
{
  typedef  myproxy<T const, T> base_t;
public:
  myproxy(T & value) : base_t(value) { }
  myproxy& operator= (T const &rhs)
  {
    std::cout << "Writing." << std::endl;
    this->m_val = rhs;
    return *this;
  }
};

template<class T>
struct mycontainer
{
  std::vector<T> my_v;
  myproxy<T> operator[] (typename std::vector<T>::size_type const i)
  {
    return myproxy<T>(my_v[i]);
  }
  myproxy<T const> operator[] (typename std::vector<T>::size_type const i) const
  {
    return myproxy<T const>(my_v[i]);
  }
};

int main()
{
  mycontainer<double> test;
  mycontainer<double> const & test2(test);
  test.my_v.push_back(1.0);
  test.my_v.push_back(2.0);
  // possible, handled by "operator=" of proxy
  test[0] = 2.0;
  // possible, handled by "operator T const& ()" of proxy
  double x = test2[0];
  // Possible, handled by "operator=" of proxy
  test[0] = test2[1];
}

印刷品

Writing
Reading
Reading
Writing

这篇关于运算符重载 C++:只写版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆