Home > Back-end >  Why am I unable to use my increment operator overload? [C ]
Why am I unable to use my increment operator overload? [C ]

Time:08-19

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 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:

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:

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?

CodePudding user response:

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. A const class method:

  1. Can only call other const class methods
  2. Cannot modify any class members
  3. If any class members are objects, only their const methods can be called

(ignoring explicitly mutable class members, to avoid confusion)

sequence effectively calls a sequence's method. As per rule 3, it is not a const class method, hence the compilation error.

You have the following options:

  • change make_message() to not be a const class method.
  • given the sample usage here, it might be feasible to declare sequence as a mutable class member. You must fully understand the repercussions of doing so.

CodePudding user response:

You are using the overloaded increment operator on a const object, but the operator itself is not const.

  •  Tags:  
  • c
  • Related