Search code examples
c++qtqlist

Input values into custom class with "<<"


I am new to C++, and I am trying to figure out how to do the following:

I have a class that holds a QList. I am trying to populate the QList as demonstrated below. I am wondering how would I achieve this? Is this done in the NumberList constructor? I normally would populate myList by using a method that takes a list of objects, and extracts them to fill the QList, but that will not work for my example below.

NumberList myList;
myList << 1 << 2 << 3;

Solution

  • Easy.

    NumberList & operator<<(NumberList & lhs, number_t rhs)
    {
        lhs.append(rhs);
        return lhs;
    }
    

    Or, as a member function, it would look like this:

    NumberList & NumberList::operator<<(number_t rhs)
    {
        append(rhs);
        return *this;
    }