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:stream &os) const;
}
std:stream &operator<<(std: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:stream &operator<<(std:stream &os, const Abs &rhs)
{
rhs.print(os);
return os;
}
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:stream &os) const;
}
std:stream &operator<<(std: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:stream &operator<<(std:stream &os, const Abs &rhs)
{
rhs.print(os);
return os;
}