multiple assigment

Hello,
i have my own vector class and I was wondering about the simpliest assigment of multiple values into it similarly to array assigment.
Is it posible something like that? It´s not necesarry have to use ()operator it could be void or anything else..
1
2
myvector test(4);
test=(1,2,3,4);

Last edited on
closed account (Dy7SLyTq)
i dont think so. you can probably do something like an initializer list
1
2
3
4
5
std::vector<int> test /*(6)*/ ;

test = { 1, 2, 3, 4 } ; // C++11

test.assign( { 1, 2, 3, 4 } ) ; // C++11 

Last edited on
In order to do as JLBorges suggests, you need to #include <initializer_list> and write an assignment operator and constructor that takes one:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class myvector
{
private:
    int* m_data;

public:
    myvector( const std::initializer_list <int> & xs )
    {
        ...
    }

    myvector& operator = ( const std::initializer_list <int> & xs )
    {
        ...
    }
};

http://www.cplusplus.com/reference/initializer_list/initializer_list/

Hope this helps.
Thank you.
Topic archived. No new replies allowed.