5

You can declare and initialize regular arrays on the same line, like so:

int PowersOfTwo[] = {1, 2, 4, 8, 16, 32, 64, 128};

Is there a way to replicate this behavior in custom classes? So, for example:

MyClass<int> PowersOfTwo = {1, 2, 4, 8, 16, 32, 64, 128};

You can have a copy constructor take an array as its parameter, but you still have to declare the array on the previous line.

int InitializationArray[] = {1, 2, 4, 8, 16, 32, 64, 128};
MyClass<int> PowersOfTwo = InitializationArray; 

2 Answers 2

6

You can implement your class in such a way that you can write this:

MyClass<int> array;
array = 1,2,3,4,5,6,7,8,9,10;//dont worry - all ints goes to the array!!!

Here is my implementation:

template <class T>
class MyClass
{
   std::vector<T> items;
public:

    MyClass & operator=(const T &item)
    {
       items.clear();
       items.push_back(item);
       return *this;
    }
    MyClass & operator,(const T &item)
    {
       items.push_back(item);
       return *this;
    }
    size_t Size() const { return items.size(); }
    T & operator[](size_t i) { return items[i]; }
    const T & operator[](size_t i) const { return items[i]; }

};

int main() {

        MyClass<int> array;
        array = 1,2,3,4,5,6,7,8,9,10;
        for (size_t i = 0 ; i < array.Size() ; i++ )
           std::cout << array[i] << std::endl;
        return 0;
}

Output:

1
2
3
4
5
6
7
8
9
10

See online demo : http://www.ideone.com/CBPmj

Two similar solutions you can see here which I posted yesterday :

Template array initialization with a list of values


EDIT:

Similar tricks you can do to populate existing STL containers. For example, you can write this:

std::vector<int> v;
v+=1,2,3,4,5,6,7,8,9,10,11,12,13,14,15; //push_back is called for each int!

All you need to overload () and , operator as:

template<typename T>
std::vector<T>& operator+=(std::vector<T> & v, const T & item)
{
    v.push_back(item); return v;
}
template<typename T>
std::vector<T>& operator,(std::vector<T> & v, const T & item) 
{
    v.push_back(item); return v;
}

Working demo : http://ideone.com/0cIUD


AGAIN EDIT:

I'm having fun with C++ operator. Now this:

std::vector<int> v;
v << 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15; //inserts all to the vector!

I think this looks better!

Sign up to request clarification or add additional context in comments.

7 Comments

I would like to see you try. I think you may need one more thing there as the list of comma separated int's does not know the type it is being assigned too and the assignment operator has the lowest priority so it will not find out until too late (I think).
@Martin: I posted the link to working code. Please see that too!
@Martin: operator,() has the lowest priority of all binary operators, the operator=() gets evaluated first, and then all the operator,() in sequence.
@Xeo: Exactly...you explained it well!
Several linear algebra libraries use similar tricks, notably Eigen: eigen.tuxfamily.org/dox-2.0/…
|
4

This can be done only if your compiler provides support for initializer lists, a C++0x feature.

Otherwise, some other syntax would have to be used, as in the boost.assign library.

1 Comment

Gotta love the quote right at the beginning of the Boost.Assign page and instantly showing a nice example: There appear to be few practical uses of operator,().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.