conversion operators ??

  • Thread starter Thread starter Chris
  • Start date Start date
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::op_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 think operator int only converts MyNumber to int but never does it backwards. If you want to assign a MyNumber with an int, use operator =

__gc class MyNumber
{
...

public: MyNumber& operator = (int d)
{
m_number = d;
return *this;
}
};

But it seems you will never get MyNumber* pDt = 10 right because you are trying to assignment the memory address 10 to a point to MyNumber. So either explicitly construct the object:

MyNumber Dt = 10;
MyNumber* pDt = new MyNumber(10); // both calling the constructor

or use a reference:

MyNumber& Dt2 = Dt;
MyNumber& Dt3 = *pDt;

Dt2 = 11;
Dt3 = 12; // both calling MyNumber::operator =(int)


The overloaded operator int will only work as this way:

int RealInteger = Dt; // same as
// int RealInteger = Dt->operator int();
// but implicitly done

ben
 
Hi Ben,

thanks for your advise but the problem is that I can't use the keyword 'operator' in a managed class (declared with __gc). I get following compiler error ==>

==> error C3258: managed operators must be declared using 'static op_<Name>' form
('operator' form is disallowed)

how do I cope with that problem ?
don't you get a compiler error ?

thanks
Chris
I think operator int only converts MyNumber to int but never does it backwards. If you want to assign a MyNumber with an int, use operator =

__gc class MyNumber
{
...

public: MyNumber& operator = (int d)
{
m_number = d;
return *this;
}
};

But it seems you will never get MyNumber* pDt = 10 right because you are trying to assignment the memory address 10 to a point to MyNumber. So either explicitly construct the object:

MyNumber Dt = 10;
MyNumber* pDt = new MyNumber(10); // both calling the constructor

or use a reference:

MyNumber& Dt2 = Dt;
MyNumber& Dt3 = *pDt;

Dt2 = 11;
Dt3 = 12; // both calling MyNumber::operator =(int)


The overloaded operator int will only work as this way:

int RealInteger = Dt; // same as
// int RealInteger = Dt->operator int();
// but implicitly done

ben
 
Chris said:
So first :
why use that odd name : 'op_Implicit' ?

This is the name that the Common Language Specification uses. So, using this
name will make the implicit conversion work in all the other languages that
try using the type.
second , more importantly
how can it make MyNumber* pDt = 10; work ?

Unfortunately, the old C++ syntax did not support doing conversions for
managed types. This is a feature we support in the new C++ syntax introduced
in Visual C++ 2005 (currently in beta).
 
Back
Top