简体   繁体   English

重载多个运算符

[英]Overloading multiple operators

Concisely, my goal is to have foo[bar] return type1, and foo[bar]= return type2. 简而言之,我的目标是让foo [bar]返回type1,而foo [bar] =返回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. 我正在用C ++编写一个对象,它的出现很顺利,但是我只希望做一件小事,但似乎不可能。

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? 那真的有用吗?

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. 没有operator[]= ,所以解决方案是编写一些带有两个关键特性的包装类: operator=接受一个值并将其设置为父容器,以及一个隐式转换运算符,它接受来自父容器并返回它。 Your operator[] will then return such wrapper. 然后,您的operator[]将返回此类包装器。

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 . 对于第二个问题,你总是可以在foo上重载operator== But maybe I misunderstood. 但也许我误会了。

If you're willing to use C++ (as your tag suggests), then most of the work has been done for you: 如果您愿意使用C ++(正如您的标记所示),那么大部分工作都已为您完成:

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM