Overloading multiple statements

In short, my goal is to foo [bar] return type1 and foo [bar] = return type2.

I am writing an object in C ++ and it is going very well, however there is only one small thing that I want to do, but that seems impossible.

My object is a storage class, so I use an array index to access values. I also need to assign, so I also overload the = operator. However, this is somewhat inconvenient, because the values ​​that my class contains are objects of the first class, so for my overloading the index of the array I cannot return them as they are. I have to return an intermediate class to handle the = operator, but I also want to get the value without additional typing.

Is there any way to do this? Hacker methods are available.

Edit: Here is an example of what he (should) do

#include<cstdio> #include<cstdlib> class foo{ char* a[100]; foo(){ for( int i = 0; i < 100; i ++) a[i] = 0; } char* operator[] (int location){ return a[location]; } foo& operator[]= (int location, const char* value ){ if( a[location] == 0 ) a[location] = (char*) malloc( strlen( value ) + 1 ); else a[location] = (char*) realloc( a[location], strlen( value ) + 1 ); strcpy( a[location], value ); } }; int main(){ foo bar; bar[20] = "Hello"; printf( "bar[20] = %s\n", bar[20] ); bar[20] = "Hello There"; printf( "bar[20] = %s\n", bar[20] ); printf( "bar[20][0] = %c\n", bar[20][0] ); } Output: bar[20] = Hello bar[20] = Hello There bar[20][0] = H 

Change again. I think I will try to formulate this in a different, but workable way. Is there a way to overload the return type when the class is referencing? So, if I have

 class foo{ bool a; bool operator /*referenced*/ (){ return a } foo& operator=(bool b){ a = b; } }; int main(){ foo a; a = b; if( a == b ) printf("It Works!"); } 

which really works?

+6
source share
2 answers

There is no operator[]= , so the solution should write some kind of wrapper class with two key functions: operator= , which takes a value and sets it to the parent container, as well as an implicit conversion operator that takes a value from the parent container and returns it. Then your operator[] will return such a wrapper.

 class foo { friend class wrapper; public: class wrapper { friend class foo; foo & _parent; int _index; wrapper(foo & parent, int index) : _index(index), _parent(parent) {} public: wrapper & operator=(const char * value) { if( _parent.a[_index] == 0 ) _parent.a[_index] = (char*) malloc( strlen( value ) + 1 ); else _parent.a[_index] = (char*) realloc( _parent.a[_index], strlen( value ) + 1 ); strcpy( _parent.a[_index], value ); return *this; } operator char *() { return _parent.a[_index]; } }; char* a[100]; foo() { for( int i = 0; i < 100; i ++) a[i] = 0; } wrapper operator[] (int location){ return wrapper(*this, location); } }; 

In the second question, you can always overload operator== to foo . But perhaps I misunderstood.

+5
source

If you want to use C ++ (as your tag suggests), then most of the work has been done for you:

 class foo: public vector<string> { public: foo() { resize(100); } }; int main() { foo bar; bar[20] = "Hello"; cout << "bar[20] = " << bar[20] << endl; bar[20] = "Hello There"; cout << "bar[20] = " << bar[20] << endl; cout << "bar[20][0] = " << bar[20][0] << endl; } 
0
source

Source: https://habr.com/ru/post/898469/


All Articles