Jump to content
  • Advertisement
Sign in to follow this  
break_r

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

This topic is 4963 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
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!