operator<< polymorphism and internationalisation

A

Adrian

Hi All,

I am trying to create an output operator that doesnt rely on the
stream type and is also polymorphic. Is there a way to combine these 2
methods so that the polymorphic output operator has template
arguments. I have tried a few ways and my compiler just wont let me.

Method 1: Doesn't rely on the char type of a stream
---------------------------------------------------
class A
{
public:
int val1;
int val2;
};

template<charT, Traits>
basic_ostream<charT, Traits> &operator<<(basic_ostream<charT, Traits>
&os, const A &rhs)
{
return os << val1 << val2;
}


Method 2: Creating polymorhpic output operator
----------------------------------------------
class Abs
{
public:
virtual ~Abs()=0 {};
virtual void print(std::eek:stream &os) const;
}

std::eek:stream &operator<<(std::eek:stream &os, const Abs &rhs)
{
rhs.print(os);
return os;
}

Tried this:
class Abs
{
public:
virtual ~Abs()=0 {};
template<charT, Traits>
virtual void print(std::basic_ostream<charT, Traits> &os) const;
// I get a : member function templates cannot be virtual : from
compiler, which makes sense, as what would go in the vtable
}

std::eek:stream &operator<<(std::eek:stream &os, const Abs &rhs)
{
rhs.print(os);
return os;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,734
Latest member
manin

Latest Threads

Top