Search code examples
c++constructorconstruction

In-explicit constructing in operator overloading?


Is it possible use in-explicit constructing with operators ?
Just like in this example (which does of course not work):

class myFoo {
    public:
        double x, y;

        myFoo(double, double);

        void    operator [] (myFoo);
};

int main() {
    myFoo f1(0.0, 1.1);
    f1[ {9.9, 10.0} ];          /// or whatever syntax to use, does not work
    f1.operator[] ( {9.9, 10.0} ); /// works !
}

Solution

  • C++11 allows an initializer list to be passed to an overloaded operator [] (see 13.5.5).

    You'd need something like

    void operator[](std::initializer_list<double>);
    

    That would match your original syntax of f1[ {9.9, 10.0} ];.