Re: Using operator() as return a reference to class self
In article <e792fd$7s0$1@news.yaako.com>, Mockey Chen
<mockey.chen@google.com> wrote:
Overload operator() is magic in C++, since you can implement
a functor using operator(), the behave like a normal function.
I found we can use a trick that operator() return a reference to
class self. According return value, we can do a recursive call
operator(), this idiom can be used to initialize a list like class,
such as list, deque, vector...
There is an example:
#include <list>
#include <iostream>
#include <iterator>
#include <algorithm>
template<typename T>
class cmlist : public std::list<T>
{
public:
// Note: here *MUST* return a reference to class self
cmlist& operator()(const T& e)
{
this->push_back(e);
return *this;
}
};
int main()
{
cmlist<int> il;
// Note: using recursive call operator to
// initialize the list.
// avoid using 3 line to call 3 push_back()
il(3)(4)(5);
std::copy(il.begin(),
il.end(),
std::ostream_iterator<int>(std::cout, " "));
return 0;
}
I don't recommend inheritance from classes without a
virtual destructor. I use an overloaded operator << to
do this. If I have a custom container class I see no major
problem.
I prefer an overload of operator << to do this in general.
The only problem with a simple template solution like
template <class T,class A,class X>
inline
std::list<T,A> & operator << (std::list<T,A> &list,const X &x)
{
list.push_back(x);
return *this;
}
occurs if X is in namespace std, but this can be handle with a wrapper
struct containing a const X &.
template <class X>
struct class_wrapper
{
const X &x;
class_wrapper(const X & a):x(a){}
operator const X &() {return x;}
};
template <class X>
inline class_wrapper<X> wrap_class(const X &x)
{ return class_wrapper<X>(x);}
and using wrap_class(x) as the args of the << chain.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]