• Advertisement
Sign in to follow this  

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

This topic is 4721 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 <tt> tags strike again! [Edited by - Washu on March 15, 2005 11:21:28 PM]

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement