samedi 30 décembre 2017

overloading [] and , c++11

So suppose we have this expression

auto x =Object.operator[][1,2,3,4];

I want my C++ code to insert the numbers 1 2 3 4 to a vector that is located inside the object my code is below.

class Object{
    Object operator[](int temp){
        this->x.push_back(temp);
        return *this;
    }
    vector<int> x;
};
int main()
{
    auto x =Object().operator[][1,2,3,4];

   return 0;
}

This code doesnt compile. As far as i have understood i have to overload comma and [] but i cant figure out how to do this.I am new to C++.

Aucun commentaire:

Enregistrer un commentaire