Click to See Complete Forum and Search --> : How to Overload "<<" operator in managed C++ class, HELP
pavanjosh
June 6th, 2006, 05:14 AM
Hello Guys,
I dont understand how to overload "<<" or ">>" operators in managed C++, ie .NET platform. If i try to do it using op_LeftShift it gives errors.
Please let me know how to do this task.
Your help be greately appriciated.
Thanks and Regards
Pavan Joshi
ThermoSight
June 6th, 2006, 01:37 PM
Boy,
there seems to be quite a bit to it.
As one might expect, each operator has a different CLS function name and that name is needed when overloading.
Here's an example ....
// ****************************************
__gc struct shipsPosition {
double latInRads;
double lonInRads;
shipsPosition():latInRads(0), lonInRads(0) {};
shipsPosition(double a, double b): latInRads(a), lonInRads(b) {};
static shipsPosition& op_Assign(shipsPosition& lhs, const shipsPosition& rhs) {
lhs.latInRads = rhs.latInRads;
lhs.lonInRads = rhs.lonInRads;
return lhs;
}
}; // end struct 'shipsPosition'
Note the "op_Assign" That's the overload for the "=" operator.
If you tell me which operator you are going to overload, I'll give you the CLS Function equivalent. Or you can (a) look it up in your tutorial, (b) Google on "CLS Function", or (c) try the IDE's 'HELP' section. The Help section may list the names, or Google may find it for you on the 'net.
OOPS! A major oversight on my part ... you DID specifically mention the operators you wanted to overload. Sorry I missed that first time around ...
the function name for "<<" is "op_LeftShift" while tha name for ">>" is "op_RightShift".
Best wishes,
bill
JamesSchumacher
June 6th, 2006, 01:53 PM
I thought that you could overload the insertion operator (left shift) normally and the compiler would generate the correct function name?
I know when I did an assignment operator overload it did make the function name op_Assign.
But that was with Visual C++ 2005 version.
I see you are still using the __gc (which with 2005 is deprecated) so I don't know if this is the case with older versions of Visual Studio .NET (2002, 2003).
ThermoSight
June 6th, 2006, 02:22 PM
Hi James.
Were you referring to my example re the __gc ?
Yeah, I believe that the 2005 edition is a terrific upgrade, and I'd sell my soul to have it, but actually I'm too poor to be able to afford the 2005 edition. When I Looked into it on Microsoft's website, I found it costs $500 (Visual Studio 2005 with C++).
My copy of VS2003 with C++ cost $110. I was SO impressed that I'd tell anyone who'd listen that it was a bargain at ten times the price .... thus prompting Microsoft to call my bluff with VS2005.
I love C++ and I **LOVE** .NET, but $500 for an upgrade of an older toy that still works well is too much for me.
re the comment about the compiler inserting the name automatically, you may very well be correct about that, but both tutorials I checked (Microsoft's Visual C++ .NET - Step By Step & Deitel's Visual C++ .NET - How To Program) offer examples with the name entered as shown (and the Microsoft book offers a list of CLS function names (suggesting to me that one may need that information) - Deitel may offer a similar list as well .... don't recall as I type this note).
Best wishes,
bill
ThermoSight
June 6th, 2006, 10:42 PM
Man,
He wasn't kiddin' .... that IS tough!
I had a few free minutes this afternoon and thought I'd solve this in a minute or two. Ha! That shift operator really is difficult.
I guess that I was just lucky that I hadn't run into it earlier. My earlier op overloads were easy compared to the left/right shift overloads.
Anyway, I did manage to get something running, but I'm not at all satisfied with what I have and I see that Microsoft's book "Visual C++ .NET - Step by Step (page 181)" goes so far as to state that OVERLOADED OPERATORS CANNOT BE CALLED IMPLICITLY FOR REFERENCE TYPES, noting that "this limitation might be removed in later versions of C++ .NET".
Then the author shows that when using reference types, "one + two" won't work, where as MyRef::op_Addition(one, two) will.
My code was a little different, using no references ....
__value struct nybble {
unsigned int datum;
nybble(void);
nybble(int source);
nybble(String *pSource);
//
// perform circular left shift of 'nybbleCnt' nybbles...
//
static unsigned int op_LeftShift(nybble source, int nybbleCnt);
};// end struct declaration 'nybble'
// struct definition .....
//
nybble::nybble() { datum = 0; }
nybble::nybble(int ijk) { datum = ijk; }
nybble::nybble(String *pSource) { datum = 0;}
unsigned int nybble::op_LeftShift(nybble source, int shiftCnt){
unsigned __int64 longform;
unsigned int intform;
int mask = 0xff; // for demo purposes; 0Xff correct for shiftCnt = 2 ONLY
longform = source.datum;
// perform circular left shift of 'n' nybbles.
longform = longform << ((shiftCnt % 8) * 4);
intform = longform & 0xFFFFFFFF;
intform |= ((longform >> 32) & mask);
return (intform);
};// end struct definition 'nybble'
// and use it here .....
//
nybble __nogc *pNybble = __nogc new nybble(0xFE03);
pNybble->datum = *pNybble << 2;
MessageBox::Show(String::Concat(S"0x",Convert::ToString((int)pNybble->datum, 16) ), S"function results",
MessageBoxButtons::OK, MessageBoxIcon::Information);
Results: (0XFE03 in returns 0XFE0300) .... (0xFE03FACE in returns 0X3FACEFE)
It is apparent that this is unfinished, and I need to play with this much, much more, but it IS an interesting problem. I'll have much more fun with this over the next coupla days. Thank you for raising the issue.
Best wishes,
bill
pavanjosh
June 8th, 2006, 02:36 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
JamesSchumacher
June 8th, 2006, 09:35 AM
std::cout is a native C++ class. (Yes, it can be used in a mixed mode application) So, in order to print out to the console what you want, you should use the System::Console class.
// visual C++ 2002 & 2003 Syntax
__gc class simple
{
public:
String *name;
int id;
simple(String *name,int id):name(name),id(id){}
};
// Visual C++ 2005 Syntax
ref class simple
{
public:
String ^name;
int id;
simple(String ^name,int id):name(name),id(id){}
};
int main(int _argc,char * _argv[])
{
// Visual C++ 2002 & 2003 syntax
simple * pObject = new simple("Name",1000);
// Visual C++ 2005 Syntax
simple ^ pObject = gcnew simple("Name",1000);
// Code below should work on all versions.
Console::WriteLine(pObject->name);
Console::WriteLine(id);
return 0;
}
codeguru.com
Copyright WebMediaBrands Inc., All Rights Reserved.