Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overloading multiple operators

Concisely, my goal is to have foo[bar] return type1, and foo[bar]= return type2.

I am writing an object in C++, and it's coming along quite nicely, however there's just one tiny little thing that I wish to do, yet it seems impossible.

My object is a storage class, so I am using an array subscript to access values. I also need to assign, so I also overload the = operator. However, it is somewhat inconvenient because the values that my class holds are first class objects, so for my array subscript overload I can't return them as is. I have to return an intermediate class to handle the = operator, but I also want to retrieve the value without additional typing.

Is there any way to do this? Hackish ways are acceptable.

Edit: Here's an example of what it (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

Edit again: I think I'll try phrasing this in a different, but workable way. Is there a way to overload the return type when a class is referenced? Such that 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!");
}

that would actually work?

like image 749
Kaslai Avatar asked Oct 02 '11 03:10

Kaslai


People also ask

Can we overload all operators?

Can we overload all operators? Almost all operators can be overloaded except a few. Following is the list of operators that cannot be overloaded. sizeof typeid Scope resolution (::) Class member access operators (.

Can you use multiple operators in C++?

C++ allows you to specify more than one definition for a function name or an operator in the same scope, which is called function overloading and operator overloading respectively.

How many operators can be overloaded?

For an operator to be overloaded, at least one of the operands must be a user-defined object. Only existing operators can be overloaded. You cannot overload new operators.

What is multiple overloading?

What Does Overloading Mean? Overloading refers to the ability to use a single identifier to define multiple methods of a class that differ in their input and output parameters. Overloaded methods are generally used when they conceptually execute the same task but with a slightly different set of parameters.


1 Answers

There is no operator[]=, so the solution is to write some sort of wrapper class with two key features: an operator= that takes a value and sets it to the parent container, and an implicit conversion operator that takes the value from the parent container and returns it. Your operator[] will then return such 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);
    }
};

For the second question, well, you could always overload operator== on foo. But maybe I misunderstood.

like image 196
Etienne de Martel Avatar answered Sep 22 '22 06:09

Etienne de Martel