C
Chris
Hi,
I want to convert an integer to an object of type 'MyNumber' using implicit
conversions.
MyNumber* pDt = 10;
but it doesn't work ??
Here's what I did :
__gc class MyNumber
{
public:
int m_number;
MyNumber(int d)
{
m_number = d;
}
// convert from int --> MyNumber
static MyNumber* op_Implicit(int d)
{
return new MyNumber(d);
}
// convert from MyNumber --> int
static int op_Implicit(MyNumber* m)
{
return m->m_number;
}
};
Then, when i try :
MyNumber* pDt = 10; // convert from int --> MyNumber
..... that doesn't work. I need to use the following syntax :
MyNumber* pDt = MyNumber:p_Implicit(10);
but hey, that's not an implicit conversion is it. Actually if I rename
'op_Implicit' to whatever name will it still work, for example renaming in
the class to 'Fx' then will the following work as well
MyNumber* pDt = MyNumber::Fx(10);
So, in my opinion isn't 'op_Implicit' doing anything else but representing
the name of a static function.
So first :
why use that odd name : 'op_Implicit' ?
second , more importantly
how can it make MyNumber* pDt = 10; work ?
thanks
Chris
I want to convert an integer to an object of type 'MyNumber' using implicit
conversions.
MyNumber* pDt = 10;
but it doesn't work ??
Here's what I did :
__gc class MyNumber
{
public:
int m_number;
MyNumber(int d)
{
m_number = d;
}
// convert from int --> MyNumber
static MyNumber* op_Implicit(int d)
{
return new MyNumber(d);
}
// convert from MyNumber --> int
static int op_Implicit(MyNumber* m)
{
return m->m_number;
}
};
Then, when i try :
MyNumber* pDt = 10; // convert from int --> MyNumber
..... that doesn't work. I need to use the following syntax :
MyNumber* pDt = MyNumber:p_Implicit(10);
but hey, that's not an implicit conversion is it. Actually if I rename
'op_Implicit' to whatever name will it still work, for example renaming in
the class to 'Fx' then will the following work as well
MyNumber* pDt = MyNumber::Fx(10);
So, in my opinion isn't 'op_Implicit' doing anything else but representing
the name of a static function.
So first :
why use that odd name : 'op_Implicit' ?
second , more importantly
how can it make MyNumber* pDt = 10; work ?
thanks
Chris