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; 
Maxpm
  • 24,113
  • 33
  • 111
  • 170

2 Answers2

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!

Community
  • 1
  • 1
Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • 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 York Mar 22 '11 at 17:56
  • @Martin: I posted the link to working code. Please see that too! – Nawaz Mar 22 '11 at 17:59
  • 1
    @Martin: `operator,()` has the lowest priority of all binary operators, the `operator=()` gets evaluated first, and then all the `operator,()` in sequence. – Xeo Mar 22 '11 at 18:00
  • @Xeo: Exactly...you explained it well! – Nawaz Mar 22 '11 at 18:01
  • Several linear algebra libraries use similar tricks, notably Eigen: http://eigen.tuxfamily.org/dox-2.0/TutorialCore.html#TutorialCoreMatrixInitialization – SuperElectric Mar 22 '11 at 18:09
  • @Maxpm: Today I'm having fun. Thanks for creating this topic. It gave me opportunity to think about various operators. Did you see my second edit by the way? – Nawaz Mar 22 '11 at 18:31
  • Perhaps I'm going overboard here, but is it possible to somehow have all the values contained in brackets? For example, `Foo = [1, 2, 3];`. I imagine it would involve overloading `operator[]` to accept some helper class that has `operator,` overloaded, but the details elude me. – Maxpm Mar 22 '11 at 19:20
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.

Cubbi
  • 46,567
  • 13
  • 103
  • 169
  • 2
    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,().` – Xeo Mar 22 '11 at 17:56