Is it possible to overload the following pairwise operator: []= . Those. index call to the internal field and write values to it.
- You can, but what exactly do you want to do? What should the operator do next? - Senior Pomidor
- a [i] = 5, i.e. assign i to the i element. 5. did I understand you correctly? - Senior Pomidor
- my goal is to set the bit to unsigned short by the index of this bit unsigned short - object field Thanks for the answer! - Konstantin Fomin
2 answers
In C ++, there is no operator like []= . But there are operators [] and = , each of which can be overloaded.
If I understand you correctly, then you mean the following
#include <iostream> class Point { private: int x; int y; public: Point( int x = 0, int y = 0 ) : x( x ), y( y ) { } const int & operator []( size_t i ) const { return i == 0 ? x : y; } int & operator []( size_t i ) { return i == 0 ? x : y; } }; int main() { Point p; p[0] = 10; p[1] = 20; std::cout << "p = { " << p[0] << ", " << p[1] << " }" << std::endl; return 0; } Console output
p = { 10, 20 } EDIT: If you are going to use this operator to manipulate bits in an integer, then look at how the std::bitset class is implemented. Since you cannot return a reference to a bit, an additional intermediate class reference is introduced, which controls the bits.
Here is how this operator is defined in std::bitset , which allows you to set the desired bit
reference operator[](size_t pos); And the reference class defines an implicit type conversion operator.
operator bool() const noexcept; For your purposes, this operator will look like
operator value_type() const noexcept; where value_type is defined as
typedef unsigned int value_type; - my goal is to set the bit to unsigned short by the index of this bit - Konstantin Fomin
- unsigned short object field - Konstantin Fomin
- @ KonstantinFomin In this case, you should see the implementation of the standard class std :: bitset. There it is done by introducing an additional reference class, since you cannot return a reference to a bit. Therefore, a reference to this class is returned, which controls the bits in the number. - Vlad from Moscow
- it is possible to make a method in the object that sets the bit (which is done at the moment). I was just puzzled - can this be achieved simply by overloading the operation? Hence the question. Thanks for the answer! - Konstantin Fomin
- I know about the bitset. but somewhat inappropriate - not that task :-) - Konstantin Fomin
As already said, the operator []= does not exist. But you can overload the operator [] and return from it an object in which you can overload the operator = , in which already and perform the necessary operations.
#include <iostream> using namespace std; class A { public: int operator =(int n) { cout << "Setting val to " << n << endl; return n; } }; class B { public: A operator [](int i) { return A(); } }; int main() { B b; int n; b[4] = 1; return 0; } Conclusion:
Setting val to 1 - You know, it’s probably easier and smarter to return links :) - Harry
- @Harry in my case the created object is not stored anywhere, so I can not return the link. I gave only an example of how this can be implemented, and the author can (and should) adjust the details for himself. - Vladimir Pavluk
- My opinion is to show better in more or less practical examples. The redefinition of the assignment is generally fraught with the fact that it can easily turn into a change in semantics ... - Harry
- @Harry is understandable, well, it means that our opinions differ :) My opinion is that the example should be the shortest worker. - Vladimir Pavluk
- my goal is to set the bit to unsigned short by the index of this bit unsigned short - object field Thanks for the answer! - Konstantin Fomin