I have read some similar questions and answers at SO, but don't really understand the answers there. My apologies, if this is a duplicate.
Have a base class like this.
class CParam
{
public:
virtual void PrintData(ostream &OutStream) = 0;
};
Now I inherit from this:
class CUInt32:public CParam
{
public:
void PrintData(ostream &OutStream);
}
void CUInt32::PrintData(ostream &OutStream)
{
// Write some data to OutStream Here
}
I overload operator << for CUInt32 class
inline ostream &operator<<(ostream &OutStream, CUInt32 &UInt32Obj)
{
UInt32Obj.PrintData(OutStream);
return (OutStream);
}
In int main() function I do the following:
int main()
{
CParam *cp = new CUInt32(ANALOG);
cout << *cp;// Error
return 0;
}
I get an error saying error: no match for 'operator<<' in 'std::cout << *cp'|
My questions are
- Is it possible to use polymorphic base class pointers with cout?
- If yes, how we could do this?
Thanks for your time!
operator <<forCUInt32, overload it forCParam. - Caseyinline ostream &operator<<(ostream &OutStream, const CParam& obj)- Artem Tokmakovvoid PrintData(ostream &OutStream)tostd::string PrintData () constand simply use it withstd::cout,printf(),fprint(), anywhere you want. - iammilind