Click here to Skip to main content
15,895,142 members
Home / Discussions / Managed C++/CLI
   

Managed C++/CLI

 
GeneralRe: Q: Changing Language with Thread::CurrenThread::CurrentUICulture Pin
led mike12-Nov-08 11:01
led mike12-Nov-08 11:01 
QuestionHelp! Program not working in 'Release mode' but works in 'Debug mode' Pin
winson you30-Oct-08 16:03
winson you30-Oct-08 16:03 
AnswerRe: Help! Program not working in 'Release mode' but works in 'Debug mode' Pin
Jonathan [Darka]31-Oct-08 4:00
professionalJonathan [Darka]31-Oct-08 4:00 
AnswerRe: Help! Program not working in 'Release mode' but works in 'Debug mode' Pin
slimtim18-Nov-08 18:51
slimtim18-Nov-08 18:51 
QuestionReporting application Pin
kasi1429-Oct-08 11:27
kasi1429-Oct-08 11:27 
AnswerRe: Reporting application Pin
Richard Andrew x6429-Oct-08 12:21
professionalRichard Andrew x6429-Oct-08 12:21 
QuestionImplicit type casts, is this possible Pin
mike toon28-Oct-08 5:04
mike toon28-Oct-08 5:04 
AnswerRe: Implicit type casts, is this possible Pin
Mark Salsbery28-Oct-08 6:33
Mark Salsbery28-Oct-08 6:33 
mike toon wrote:
In unmanged code it seemed good enough to overload the type cast operators for this to happen but it appears that this is not the case in managed c++.


Do you have an example where this isn't the case?

This still works in C++/CLI:
ref class numericstring
{
    String ^str;
public:
    numericstring()
    {
        str = gcnew String("123.456");
    }

    operator bool() {return Convert::ToDouble(str) != 0;}
    operator int() {return Convert::ToInt32(Convert::ToDouble(str));}
    operator double() {return Convert::ToDouble(str);}
};


[STAThreadAttribute]
int _tmain()
{
    numericstring ns;
    bool nsbool = ns;
    int nsint = ns;
    double nsdouble = ns;
...

This form works as well
ref class numericstring
{
    String ^str;
public:
    numericstring()
    {
        str = gcnew String("123.456");
    }

    static operator bool(numericstring^ val) {return Convert::ToDouble(val->str) != 0;}
    static operator int(numericstring^ val) {return Convert::ToInt32(Convert::ToDouble(val->str));}
    static operator double(numericstring^ val) {return Convert::ToDouble(val->str);}

};


[STAThreadAttribute]
int _tmain()
{
    numericstring ^ns = gcnew numericstring();
    bool nsbool = ns;
    int nsint = ns;
    double nsdouble = ns;
...

In both versions above, implicit is implied, but you can add the explicit
keyword to force explicit conversion semantics (which in the above examples
would be a good idea Smile | :) )...
ref class numericstring
{
    String ^str;
public:
    numericstring()
    {
        str = gcnew String("123.456");
    }

    static explicit operator bool(numericstring^val) {return Convert::ToDouble(val->str) != 0;}
    static explicit operator int(numericstring^val) {return Convert::ToInt32(Convert::ToDouble(val->str));}
    static explicit operator double(numericstring^val) {return Convert::ToDouble(val->str);}

};


[STAThreadAttribute]
int _tmain()
{
    numericstring ^ns = gcnew numericstring();
    bool nsbool = (bool)ns;
    int nsint = (int)ns;
    double nsdouble = (double)ns;
...

See Changes to Conversion Operators[^]

Mark

Mark Salsbery
Microsoft MVP - Visual C++

Java | [Coffee]

GeneralRe: Implicit type casts, is this possible [modified] Pin
mike toon29-Oct-08 1:36
mike toon29-Oct-08 1:36 
GeneralRe: Implicit type casts, is this possible Pin
Mark Salsbery29-Oct-08 5:22
Mark Salsbery29-Oct-08 5:22 
GeneralRe: Implicit type casts, is this possible Pin
mike toon30-Oct-08 0:52
mike toon30-Oct-08 0:52 
GeneralRe: Implicit type casts, is this possible Pin
mike toon30-Oct-08 6:03
mike toon30-Oct-08 6:03 
GeneralRe: Implicit type casts, is this possible Pin
led mike30-Oct-08 6:13
led mike30-Oct-08 6:13 
GeneralRe: Implicit type casts, is this possible [modified] Pin
Mark Salsbery30-Oct-08 9:08
Mark Salsbery30-Oct-08 9:08 
GeneralRe: Implicit type casts, is this possible Pin
mike toon30-Oct-08 23:52
mike toon30-Oct-08 23:52 
QuestionI need help 1 items on the program administrators: (run on DOS) Pin
qt_oct26-Oct-08 19:22
qt_oct26-Oct-08 19:22 
AnswerRe: I need help 1 items on the program administrators: (run on DOS) Pin
Paul Conrad27-Oct-08 4:55
professionalPaul Conrad27-Oct-08 4:55 
AnswerCP IGNORE: homework Pin
leckey27-Oct-08 10:05
leckey27-Oct-08 10:05 
QuestionIncrementally convert existing native MFC Application to .NET Pin
Rolf Kristensen24-Oct-08 14:16
Rolf Kristensen24-Oct-08 14:16 
AnswerRe: Incrementally convert existing native MFC Application to .NET Pin
StevenS_Dev28-Oct-08 4:35
StevenS_Dev28-Oct-08 4:35 
GeneralRe: Incrementally convert existing native MFC Application to .NET [modified] Pin
Rolf Kristensen28-Oct-08 5:10
Rolf Kristensen28-Oct-08 5:10 
Questionconst char* to char* conversion Pin
auralius manurung23-Oct-08 20:10
auralius manurung23-Oct-08 20:10 
AnswerRe: const char* to char* conversion Pin
Mark Salsbery23-Oct-08 20:23
Mark Salsbery23-Oct-08 20:23 
AnswerRe: const char* to char* conversion Pin
KarstenK23-Oct-08 21:10
mveKarstenK23-Oct-08 21:10 
GeneralRe: const char* to char* conversion Pin
auralius manurung23-Oct-08 22:24
auralius manurung23-Oct-08 22:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.