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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top