473,322 Members | 1,431 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

conversion operators ??

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

Nov 17 '05 #1
3 1062
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,

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


Nov 17 '05 #2
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
"benben" <be******@yahoo.com.au> wrote in message news:OD**************@TK2MSFTNGP09.phx.gbl...
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,

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


Nov 17 '05 #3
Chris wrote:
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).

--
Brandon Bray, Visual C++ Compiler http://blogs.msdn.com/branbray/
This posting is provided AS IS with no warranties, and confers no rights.
Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: BigMan | last post by:
Here is a piece of code: #include <memory> using namespace std; template< typename SomeType > void f(auto_ptr_ref< SomeType >) { }
5
by: Vijai Kalyan | last post by:
Hello, I have come back to C++ after a couple of years with Java so I am quite rusty and this question may seem poor: My platform is Windows XP with MSVC 7.1. I have a class with a...
2
by: Alex Sedow | last post by:
Why explicit conversion from SomeType* to IntPtr is not ambiguous (according to standart)? Example: // System.IntPtr class IntPtr { public static explicit System.IntPtr (int); public...
3
by: Egbert Nierop \(MVP for IIS\) | last post by:
Example... This would make that i can assign a ULONG to a CComBSTR2 class while the body of the code performs necessary conversion. CComBSTR2& operator=(ULONG ulong) {
4
by: Påhl Melin | last post by:
I have some problems using conversion operators in C++/CLI. In my project I have two ref class:es Signal and SignalMask and I have an conversion function in Signal to convert Signal:s to...
1
by: hunter hou | last post by:
Hello,Please look at the following code(from C++ in a nutshell) and my questions.Thanks,***Hunter... typedef void (*strproc)(const char*); void print(const char* str) { std::cout << "const...
14
by: Richard G. Riley | last post by:
Would it be wrong to use "implicit casting" instead of the standards "implicit conversion" when talking about implicit conversions between certain data types. The standard mentions "explicit...
2
by: =?Utf-8?B?U2FtZWVrc2hh?= | last post by:
Suppose there are 2 classes A and B with a int parameter called 'val' in each. Both of them provide a public constructor with int type parameter. Suppose class A provides a explicit conversion...
4
by: subramanian100in | last post by:
In the book, C++ Coding Standards book by Hereb Sutter and Andrei Alexandrescu, in Item 40 on pages 86-87 viz, "Avoid providing implicit conversions", the authors have advised the use of named...
7
by: Fraser Ross | last post by:
I was looking at the new explicit conversion operators in the standard. The writing at the start of 12.3.2/2 doesn't make sense. "A conversion function may be explicit (7.1.2), in which case it is...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.