C# overloaded operators not working from Managed C++

D

dbwNick

I have some classes written in C# that work fine from Managed C++ and
Visual Basic .NET in all areas except for operator overloading (+, -,
*). I fully understand that I need to offer a secondary method for
Visual Basic to use as it does not support operator overloading.
However I would expect it to work in C++
Instead I get the following error on compilation:

E:\C#>CL democpp.cpp /clr
Microsoft (R) C/C++ Standard Compiler Version 13.00.9466 for .NET
Framework
Copyright (C) Microsoft Corporation 1984-2001. All rights reserved.

democpp.cpp
democpp.cpp(21) : error C2296: '*' : illegal, left operand has type
'Modules::cModule_256K __gc *'
democpp.cpp(21) : error C2297: '*' : illegal, right operand has type
'Modules::cModule_256K __gc *'

I reference the C# DLLs with these lines:
#using <encModules.dll>
using namespace Modules;

Any help on this subject much appreciated!

Nick
 
B

Brandon Bray [MSFT]

dbwNick said:
I have some classes written in C# that work fine from Managed C++ and
Visual Basic .NET in all areas except for operator overloading (+, -,
*). I fully understand that I need to offer a secondary method for
Visual Basic to use as it does not support operator overloading.
However I would expect it to work in C++

Unfortunately, operator overloading for managed types is not easily
accessible in C++. This is because __gc class types can only be created as
__gc pointers, and operator overloading does not work on pointers. You can
bind an instance to a __gc& and then try calling the operator on the
reference. A better solution is simply to call the op_* function directly
(i.e. op_Addition, op_Subtraction, etc.)

I have to admit, this is really frustrating. We're very focused on improving
this in the next release of Visual C++. I'm sorry that we don't have
anything better right now.

Cheerio!
 

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

Top