简体   繁体   中英

C++ [] index operator overloading

Base.h

#pragma once
class Base {
protected:
    std::string name;

public:
    Base() {
        //something
    }
    Base(std::string name) {
        //something
    }

    std::string getName() {
        return this->name;
    }

    void setName(std::string name) {
        this->name = name;
    }
};

Derived1

#pragma once
#include "Base.h"
class Derived1 : public Base {
protected:
    int other;

public:
    Derived1() {
        //something
    }
    Derived1(int other) {
        //something
    }
};

Derived2.h

#pragma once
#include "Derived1.h"
class Derived2 : public Derived1 {
protected:
    int other;

public:
    Derived2() {
        //something
    }
    Derived2(int other) {
        //something
    }
};

Derived3.h

#pragma once
#include "Derived2.h"
class Derived3 : public Derived2 {
protected:
    int other;

public:
    Derived3() {
        //something
    }
    Derived3(int other) {
        //something
    }
};

Foo.h

#include <vector>
#include <string>

#include "Base.h"
#include "Derived1.h"
#include "Derived2.h"
#include "Derived3.h"

class Foo {
private:
    std::vector<Base*> Vect;

public:
    Foo() {
        //something
    }

template<typename T>
T& operator[](std::string name) {
    bool found = false;
    int index = -1;

    for (int i = 0; i < Vect.size(); i++) {
        if (Vect[i]->getName() == name) {
            found = true;
            index = i;
        }

        if (found == true) {
            break;
        }
    }

    return Vect[index];
}
};

Source.cpp

Foo foo;
std::string x = "TEXT";
std::cout << foo[x];

I have a class Foo with a vector of Base* class pointers . I'm trying to overload the [] (index, subscript) operator for Foo , so that I give a string as an input and it returns one element from Vect whose private member name matches the input. The element that can be returned can be either of Derived1 , Derived2 , Derived3 , that's why I tried to make it templated.

However, in the source file I get these errors saying

no operator "[]" matches these operands
   operand types are: Foo[std::string]
Foo does not define this operator or a conversion to a type acceptable to the predefined operator

operator[] should be a non-static member function with exactly one parameter. Template functions where the template parameter is used as the return type do not work, because the standard invocation of foo[x] does not allow the compiler to infer the template type.

To invoke your templated operator, you'd need to do

foo.operator[]<Base>(x)

which is very verbose.

Remove the template stuff and change the return type of your operator to be a Base & .

Base& operator[](std::string name)
{
    // ...
    return *Vect[index]
}

I've also fixed your return statement.

Note that there are other improvements that can be made to your operator[] , and you don't handle the case of the subscript value not being found in your vector (you'll try to reference Vect[-1] , which will be undefined behavior).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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