implicit conversion ( quite newbie ) 
Author Message
 implicit conversion ( quite newbie )

Hi all !

I have defined class _Window which is supposed to be mini
replacement of CWnd. In order to pass objects of type
_Window to the Windows functions requiring HWND I have
defined public conversion operator as follows

        operator HWND() {return m_hWnd;};
        // m_hWnd - pivate member of type HWND

now, when I try to write such code

if (!TranslateAccelerator(m_pFrmWnd,(HACCEL) NULL,&msg))...
(m_pFrmWnd is pointer to object created from _Window's
descendant type)

I get following error

Compiling...
App.cpp
c:\program\cpp\common\app.cpp(67) : error
C2664: 'TranslateAcceleratorA' : cannot convert parameter
1 from 'class _FrmWnd *' to 'struct HWND__ *'
        Types pointed to are unrelated; conversion
requires reinterpret_cast, C-style cast or function-style
cast
Error executing cl.exe.

App.obj - 1 error(s), 0 warning(s)

but when I change to

if (!TranslateAccelerator(reinterpret_cast<HWND>
(m_pFrmWnd),(HACCEL) NULL,&msg))...

it compiles fine

Could you please guide me, what I'm missing ???

Damian()



Sun, 27 Mar 2005 22:18:48 GMT  
 implicit conversion ( quite newbie )

Quote:

> Could you please guide me, what I'm missing ???

You're missing that user defined conversions apply to objects, not pointers
to objects.  Pass *m_pFrmWnd instead of m_pFrmWnd and your code will
actually invoke your user-defined conversion operator.

Note that the code with the reinterpret_cast may compile, but it will not
run, as your m_pFrmWnd is effectively an HWND*, which you are casting to
HWND - not what you wanted.

Also, don't name your class _Window.  All names begining with underscore are
reserved to the implementation.

-cd



Sun, 27 Mar 2005 23:08:37 GMT  
 implicit conversion ( quite newbie )

Quote:

> I have defined class _Window which is supposed to be mini
> replacement of CWnd.

Note that _Window is a reserved identifier, because it begins
with an underscore followed by an uppercase letter.

In class _Window:

Quote:
> operator HWND() {return m_hWnd;};
>         // m_hWnd - pivate member of type HWND

(You should not have a semicolon after a brace that closes a
block of statements.)

This will convert _Window& to HWND...

Quote:
> if (!TranslateAccelerator(m_pFrmWnd,(HACCEL) NULL,&msg))...
> (m_pFrmWnd is pointer to object created from _Window's
> descendant type)

...but m_pFrmWnd is a _FrmWnd*, and there is no implicit
conversion from that to _Window&.  It can be converted to
_Window* but that does not help.

If you used *m_pFrmWnd instead, then the type would be
_FrmWnd&, which would get converted to _Window&
and from there to HWND.

Quote:
> if (!TranslateAccelerator(reinterpret_cast<HWND>
> (m_pFrmWnd),(HACCEL) NULL,&msg))...

> it compiles fine

Sure it does, but the HWND will be totally wrong.


Sun, 27 Mar 2005 23:26:20 GMT  
 implicit conversion ( quite newbie )
thank you guys, both!!!

I've just realised that I have VERY poor notion of what *
and & operators mean on the left side of sybol, what on
the right, what on the left side of '=', and so on.
I think I have to excersise with simple examples in the
de{*filter*} instead prompt posting to this groups.
Help of such gurus makes one simply to lazy ...

Damian

Quote:
>-----Original Message-----
>Hi all !

>I have defined class _Window which is supposed to be mini
>replacement of CWnd. In order to pass objects of type
>_Window to the Windows functions requiring HWND I have
>defined public conversion operator as follows

>    operator HWND() {return m_hWnd;};
>        // m_hWnd - pivate member of type HWND

>now, when I try to write such code

>if (!TranslateAccelerator(m_pFrmWnd,(HACCEL)
NULL,&msg))...
>(m_pFrmWnd is pointer to object created from _Window's
>descendant type)

>I get following error

>Compiling...
>App.cpp
>c:\program\cpp\common\app.cpp(67) : error
>C2664: 'TranslateAcceleratorA' : cannot convert parameter
>1 from 'class _FrmWnd *' to 'struct HWND__ *'
>        Types pointed to are unrelated; conversion
>requires reinterpret_cast, C-style cast or function-style
>cast
>Error executing cl.exe.

>App.obj - 1 error(s), 0 warning(s)

>but when I change to

>if (!TranslateAccelerator(reinterpret_cast<HWND>
>(m_pFrmWnd),(HACCEL) NULL,&msg))...

>it compiles fine

>Could you please guide me, what I'm missing ???

>Damian()
>.



Tue, 29 Mar 2005 18:25:53 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. implicit conversion to/from an interface not allowed??

2. Implicit conversions

3. explicit / implicit conversion in managed C++

4. Implicit Conversion Disallowed

5. Implicit conversion question

6. ANSI C++ forbids implicit conversion from

7. Hi-tech C compiler : implicit signed to unsigned conversion (warning)

8. implicit type conversion

9. implicit conversion

10. Implicit type conversion in VC6 and VC7-2003

11. How can I avoid implicit conversion?

12. Implicit conversion to ostream?

 

 
Powered by phpBB® Forum Software