[英]Why am I unable to use my increment operator overload? [C++]
I have the following struct:我有以下结构:
struct sequence_t {
uint8_t val;
explicit sequence_t(uint8_t value) : val(value) {}
sequence_t() : sequence_t(0) {}
auto operator++() -> sequence_t& { // prefix
val = (val + 1) % 16;
return *this;
}
auto operator++(int) -> sequence_t { // postfix
sequence_t tmp{val};
++*this;
return tmp;
}
uint8_t value() const { return val; }
auto operator==(const sequence_t& other) const -> bool = default;
auto operator==(const uint8_t other) const -> bool { return val == other; }
};
And I use it inside a class declared like this:我在这样声明的 class 中使用它:
class messenger {
private:
sequence_t sequence;
public:
messenger() = default;
~messenger() = default;
auto make_message(const uint8_t type) const -> std::shared_ptr<std::uint8_t[]>;
auto make_message(uint8_t const* data, const uint8_t size) const -> std::shared_ptr<std::uint8_t[]>;
auto parity(uint8_t const* buffer) const -> std::uint8_t;
};
I am calling the operator in the make_message() member of the messenger class because I want to update the value of the sequence (to the whole messenger object) when I create a message:我在信使 class 的 make_message() 成员中调用运算符,因为我想在创建消息时更新序列的值(到整个信使对象):
auto messenger::make_message(uint8_t const* data, const uint8_t data_size) const -> std::shared_ptr<std::uint8_t[]> {
auto buffer = std::make_shared<std::uint8_t[]>(sizeof(header) + data_size + sizeof(parity(nullptr)));
++sequence;
header h = {START, data_size, sequence.value(), TYPE_DATA}; // TODO: implementar sequência
std::copy(std::bit_cast<uint8_t*>(&h), std::bit_cast<uint8_t*>(&h) + sizeof(header), buffer.get());
std::copy(data, data + data_size, buffer.get() + sizeof(header));
buffer[sizeof(header) + data_size] = parity(buffer.get());
return buffer;
}
But when I try to use sequence++ or ++sequence inside the messenger class methods I get the following error:但是,当我尝试在信使 class 方法中使用 sequence++ 或 ++sequence 时,我收到以下错误:
error: passing ‘const sequence_t’ as ‘this’ argument discards qualifiers [-fpermissive]
[build] 17 | ++sequence;
[build] | ^~~~~~~~
Why is it const?为什么是常量? How can I modify the content of my sequence?
如何修改序列的内容?
auto messenger::make_message(uint8_t const* data, const uint8_t data_size) const
That const
keyword at the end signifies that this is a const
class method.最后的
const
关键字表示这是一个const
class 方法。 A const
class method:一个
const
class 方法:
const
class methodsconst
class 方法const
methods can be calledconst
方法(ignoring explicitly mutable
class members, to avoid confusion) (忽略显式
mutable
class 成员,以避免混淆)
++sequence
effectively calls a sequence
's method. ++sequence
有效地调用了sequence
的方法。 As per rule 3, it is not a const
class method, hence the compilation error.根据规则 3,它不是
const
class 方法,因此会出现编译错误。
You have the following options:您有以下选择:
make_message()
to not be a const
class method.make_message()
更改为不是const
class 方法。sequence
as a mutable
class member.sequence
声明为mutable
class 成员可能是可行的。 You must fully understand the repercussions of doing so. You are using the overloaded increment operator on a const
object, but the operator itself is not const
.您在
const
object 上使用重载增量运算符,但运算符本身不是const
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.