C++11 如何从只读中删除对象
我正在做一个作业,我有一个小问题,我应该写的函数,这里是函数的说明C++11 如何从只读中删除对象,c++11,copy-constructor,C++11,Copy Constructor,我正在做一个作业,我有一个小问题,我应该写的函数,这里是函数的说明 创建一个名为“value”的无符号字符变量,并将其分配给1 使用左移位位运算符确定设置偏移标识的位 使用按位AND和DATAM_u以及上一个项目符号的结果 如果上一个项目符号的答案为!=0. 第一个错误是: error: assignment of member ‘bool_array<13ul>::bit_proxy::datum_’ in read-only object datum_ &= (
error: assignment of member ‘bool_array<13ul>::bit_proxy::datum_’ in read-only object
datum_ &= (value << offset_);
错误:在只读对象中分配成员“bool\u数组::bit\u代理::datum\u”
数据&=(值位&&a.bits,sizeof(无符号字符)*MAX);
}
//复印作业
布尔_数组和运算符=(布尔_数组常量和a){
std::memcpy(this->bits_u,&a.bits_u,sizeof(unsigned char)*MAX);
归还*这个;
}
//析构函数
~bool_数组()=默认值;
//获取对size()的访问权限
constexpr size_type size()常量{
返回N;
}
类位代理{
私人:
无符号字符数据;
尺寸\u类型常数偏移量\uu;
公众:
bit_proxy()=删除;
//默认复制构造函数和分配
位代理(位代理常量&)=默认值;
位代理和运算符=(位代理常量-)=默认值;
//默认移动构造函数和赋值
位代理(位代理&&)=默认值;
位代理和运算符=(位代理和运算符)=默认值;
//默认删除
~bit_proxy()=默认值;
位代理(无符号字符和基准,大小类型偏移):基准(基准),偏移(偏移){
}
位\代理和运算符=(布尔b)
{
无符号字符值=1;
如果(b)
datum|=(value你的函数操作符bool()
被定义为常量(不修改对象的内容),但你更改了一个成员变量。要么删除常量
,要么不修改datum这是不是意味着要在那里旋转?datum&=~(赋值表示“使用按位和”。尝试使用按位and而不是&=
。后者不是完全的按位and运算符。提示:按位and没有副作用,您需要以某种方式使用其结果
#include <limits>
#include <cstring>
#include <cstddef>
#include <iostream>
template <std::size_t N>
class bool_array{
public:
using size_type = std::size_t;
using value_type = bool;
//default constructor
bool_array(){
std::memset(bits_, 0, sizeof(unsigned char)*MAX);
}
//copy constructor
bool_array(bool_array const& a){
std::memcpy(this->bits_, &a.bits_, sizeof(unsigned char)*MAX);
}
//copy assignment
bool_array& operator = (bool_array const& a){
std::memcpy(this->bits_, &a.bits_, sizeof(unsigned char)*MAX);
return *this;
}
//destructor
~bool_array() = default;
//getting access to size()
constexpr size_type size() const{
return N;
}
class bit_proxy{
private:
unsigned char datum_;
size_type const offset_;
public:
bit_proxy() = delete;
//default copy constructor & assigment
bit_proxy(bit_proxy const&) = default;
bit_proxy& operator = (bit_proxy const&) = default;
//default move constructor & assignment
bit_proxy(bit_proxy&&) = default;
bit_proxy& operator = (bit_proxy&&) = default;
//default delete
~bit_proxy() = default;
bit_proxy(unsigned char& datum, size_type offset) : datum_(datum), offset_(offset){
}
bit_proxy& operator = (bool b)
{
unsigned char value = 1;
if(b)
datum_ |= (value << offset_);
else
datum_ &=~ (value << offset_);
return *this;
}
/*******************This is the function with the error**************************/
operator bool() const
{
unsigned char value = 1;
datum_ &= (value << offset_);
//datum_ &= offset_;
if(datum_ != 0)
return true;
}
/*********************************Function ends here************************************************/
};
bit_proxy operator [](size_type i)
{
return bit_proxy{ bits_[i / std::numeric_limits<unsigned char>::digits],
i % std::numeric_limits<unsigned char>::digits }
;
}
private:
static constexpr auto MAX = N / std::numeric_limits<unsigned char>::digits + (N % std::numeric_limits<unsigned char>::digits != 0);
unsigned char bits_[MAX];
};
int main(){
using namespace std;
bool_array<13> test;
for(unsigned i=0; i !=13; ++i)
cout << i << " : " << test[i] << '\n';
for(unsigned i=0; i != 13; ++i){
cout << i << " : " << test[i] << "; ";
test[i] = true;
cout << test[i] << '\n';
}
for(unsigned i=0; i!= 13; ++i)
cout << i << " : " << test[i] << '\n';
for(unsigned i=0; i!= 13; ++i){
cout << i << " : " << test[i] << "; ";
test[i] = false;
cout << test[i] << '\n';
}
for(unsigned i=0; i != 13; ++i)
cout << i << " : "<< test[i] << '\n';
cout << "\n\n";
}