Click to See Complete Forum and Search --> : Overload "<<" for cout of an object


pavanjosh
June 8th, 2006, 02:58 AM
Hello All,

Thanks very much for the support. But the problem is something like this

In managed C++ how do i make this happen,

say i have a class something like this

__gc class simple
{

public:
String *name;
int id;
simple(String *name,int id):name(name),id(id){}
};

int _tmain()
{

simple *s = new simple("xyz",100);

cout << s;
}

Now do i make that happen?
It is troubling me a lot

Please help if somebody finds a solution

Thanks
Pavan Johsi

Rich2189
June 8th, 2006, 08:14 AM
I may be wrong but in the public section put


void operator <<(){cout << name;}

cilu
June 8th, 2006, 05:47 PM
Nope, that won't work. You can only override a set of operators for managed classes, by using the op_<OperatorType> form.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore/html/vctbsCompilerErrorC3258.asp

Well, you should override ToString().

Mitsukai
June 8th, 2006, 07:14 PM
you can.

you will have to replace cout with the type cout is using.

cout& operator <<(cout& p_Stream, simple& p_ToCout)
{
cout << p_ToCout.name;
return(p_Stream);
}