Original Post
I want to define operator<< so that I can do std::cout << (*e) << std::endl; EventArg is a templated class which derives from EventArgBase The code at the bottom should print "foo (arg)" but instead prints "foo ()". Soooo... what did I do wrong? I thought I had this :( [Edited by - thedustbustr on December 23, 2004 10:57:33 PM]
class EventArgBase
{
public:
//void operator()(){} //void does not have operator<< defined
std::string operator()(){return "";}
virtual std::string dump()
{ std::stringstream s; return s.str(); }
};
template<typename Elem, typename Traits>
std::basic_ostream<Elem,Traits>& operator<<
(std::basic_ostream<Elem,Traits>& out, const EventArgBase& in)
{return out << "(" << in() << ")";}
template<typename T>
class EventArg : public EventArgBase
{
public:
T t;
EventArg(T _t) {t=_t;}
T operator()() {return t;}
std::string dump()
{ std::stringstream s; s<<(*this)(); return s.str(); }
};
class Event
{
public:
std::string type;
double time;
double true_time;
EventArgBase* arg;
Event(std::string t, EventArgBase* a=new EventArgBase(), double when=0);
~Event();
};
template<typename Elem, typename Traits>
std::basic_ostream<Elem,Traits>& operator<<
(std::basic_ostream<Elem,Traits>& out, const Event& in)
{return out << in.type << " (" << (*in.arg)() << ")" ;}
/*...*/
Event* e = new Event("foo", new EventArg<std::string>("arg"));
std::cout << (*e) << std::endl;