Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I inherited some C++ code and it has me stumped.

Have a class declared like so.
C++
class CIENavigator :  public CWTLAxControl<CIENavigator,iWebBrowser2>
{
public:
...

Instance like so
CIENavigator CIE;
CIENavigator * pCIE = &CIE;


Gets this compile error
C++
1>.\IENavigator.cpp(446) : error C2440: 'initializing' : cannot convert from 'IWebBrowser2 **' to 'CIENavigator *'
1>        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast


Although CIE is declared as CIENavigator, it thinks the type of &CIE to be IWebBrowser2 **

If I force the cast like so:
C++
CIENavigator CIE;
CIENavigator * pCIE = (CIENavigator *) &CIE;

It compiles, but the pointer is indeed the wrong pointer.

I note the class name appears in the declaration of itself, but I don't know what the significance of that is.
I'm not up on Class templates.

How do I get the right CIENavigator object (CIE) address?
Posted
Updated 21-Apr-12 11:04am
v3
Comments
Nelek 21-Apr-12 17:05pm    
Added code tags
xComaWhitex 21-Apr-12 19:18pm    
Why are you even using C style casting, which are error prone and doesn't show your intention on casting? I think you need to read up on casting.
Nelek 22-Apr-12 18:34pm    
Was it a comment for me?
xComaWhitex 22-Apr-12 18:37pm    
Yeah?

1 solution

Answer is the & operator was overloaded to return a different type.
from stackoverflow.com.

Also, since the & operator did not produce the address of the object, a workaround:

CLASSfoo foo[1];
CLOSSfoo *pfoo = foo; //since C still treats arrays as pointer to first element.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900