Skip to main content
GameDev.net gamedev.net
🔒 Locked

[.net] C++/CLI Implilcit conversion operator

Started by break_r Mar 15, 2005 at 10:54 PM 1 replies 850+ views
Original Post
break_r
break_r
Hi, I have the following console app:

using namespace System;

class Foo
{
    double d;

public:
    Foo() : d( 3.14159 ) {  }

    operator double()
    {
        return d;
    }
};

int main()
{
    Foo f;
    double e = f;

    Console::WriteLine( e );  // outputs 3.14159 and everthing is good!
    Console::ReadLine();
}

Now when I change Foo to a managed type: ref class Foo { ... } and run the same program, I get 5.91378815174249E+276 Anyone know how implicit conversion operators should be implemented with the new C++/CLI syntax? Thx. [edit] Washu: The amazing [ source] and tags strike again! [Edited by - Washu on March 15, 2005 11:21:28 PM]
TheBluMage
TheBluMage
Simply changing the class to a managed class with the ref keyword left several compile issues that needed resolving for me. In managed classes the conversion operators are written differently. Change this:

operator double() {
return d;
}


To this:

static operator double(Foo^ foo) {
return foo->d;
}


And change this:

Foo f;

To this:

Foo^ f = gcnew Foo();


That worked as expected for me anyway.
break_r
break_r
Thank you very much.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.