#Changing the ''='' operator in a class?

8 messages · Page 1 of 1 (latest)

restive storm
#

Hello

So I want to make a Set class, and I want it to be initialized like this:

int main() {
  Set N = {1, 2, 3, 4, 5};
}

is there any way to do it, ik there is a way to do it like:

Set N {1, 2, 3, 4, 5};

if its impossible pls just tell me, but for now im gonna hunt for this in the constructor documentation

keen mantleBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.

crude perch
#

You need to overload the copy constructor for std::initializer_list

#
class Set
{
public:
    Set(std::initializer_list<int> il)
    {
        elements.resize(il.size());

        for(std::size_t i = 0; i < il.size(); i++)
        {
            int val = *(il.begin() + i);
            elements[i] = val;
        }
    }

private:
    std::vector<int> elements;
};

int main () 
{
    Set N = {1, 2, 3, 4, 5};
}
restive storm
#

thank you very much this helped a lot

#

i just need to do some digging in the initializer list thing and make my own dynamic array

#

thanks

#

!solved