C++ 运算符重载C++;:只写版本
我正在重载数据结构的运算符,因此我有标准函数声明:C++ 运算符重载C++;:只写版本,c++,overloading,operator-keyword,C++,Overloading,Operator Keyword,我正在重载数据结构的运算符,因此我有标准函数声明: T & operator[](int i); //used for regular objects const T & operator[](int i) const; // used for const objects 所以我要做的是为常规对象提供两个版本的操作符[]:一个是当操作符[]用于写入而不是读取时,它会执行不同的操作 我一直在读,这是可能的,但我还没有看到任何代码 我见过很多次这个问题被问到,我也见过答案“
T & operator[](int i); //used for regular objects
const T & operator[](int i) const; // used for const objects
所以我要做的是为常规对象提供两个版本的操作符[]:一个是当操作符[]用于写入而不是读取时,它会执行不同的操作
我一直在读,这是可能的,但我还没有看到任何代码
我见过很多次这个问题被问到,我也见过答案“'operator[]const'version用于阅读”-->但这不是真的;它仅用于类的常量实例化
是否有人可以提供有关检测写入事件以触发不同行为的指导?
复制构造函数中的技巧可能是什么?正如一些人在评论中提到的,您需要返回一个代理,也称为智能引用,它是您想要的实际类型T的包装器,并且有一个指向调用[]运算符的原始对象的链接 当代理位于=的左侧时,编译器将调用其赋值运算符以键入T。 当代理位于=的右侧时,编译器将调用其强制转换运算符以键入T
这是一个字符串例子,我想Scott Meyer的C++有效 main的第一行应该打印“写入字符串” 第二行将打印“从字符串读取”
持有对象的类无法获取您对返回对象的访问是读访问还是写访问的信息 通过成员函数限定符,只有对象本身具有“我在哪个上下文中使用”的概念
- Ref限定符
- 常量/可变限定符
#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];
}
#包括
#包括
#包括
模板
类myproxy
{
受保护的:
乌姆瓦尔;
myproxy&运算符=(myproxy常量&)=delete;
公众:
myproxy(U&value):m_val(value){}
算子T&()
{
STD::AFUAK。C++中唯一的方法是返回代理值。可能的副本是:当const版本是rValk时,另一个是LValk,如果不是编译器可能有问题的话。你能给出一个小代码示例吗?这里不是这样的。@ Surt,不,const版本是使用的。对const
对象(或通过const
限定引用等)调用运算符。与左值和右值无关。
#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