|
-
February 3rd, 2008, 06:17 AM
#1
C-style cast suceeds whereas static_cast reports error
I am using Visual Studio 2005:
The following code fails:
Code:
using namespace NepwareGames::MainApplication;
CApplicationCallback *pWin = static_cast<CApplicationCallback *>(new CWindow);
The error reported is
Code:
1>j:\nepwaregames\nepwaregames\nepwaregames.cpp(28) : error C2243: 'static_cast' : conversion from 'NepwareGames::MainApplication::CWindow *' to 'NepwareGames::MainApplication::CApplicationCallback *' exists, but is inaccessible
However the C-style cast works fine and the program executes as expected.
The following are the declaration of the classes:
Code:
namespace NepwareGames
{
namespace MainApplication
{
class CApplicationCallback
{
public:
// pure virtual functions
virtual ~CApplicationCallback(void)= 0 {}
virtual bool CreateEx(DWORD dwExStyle, LPCTSTR lpszClass, LPCTSTR lpszName, DWORD dwStyle,
int x, int y, int nWidth, int nHeight, HWND hParent,
HMENU hMenu, HINSTANCE hInst) = 0;
virtual WPARAM MessageLoop(void) = 0;
virtual BOOL ShowWindow(int nCmdShow) const = 0;
virtual BOOL UpdateWindow(void) const = 0;
};
class CApplication
{
public:
static CApplication * Create (CApplicationCallback *par);
};
}
}
Code:
namespace NepwareGames
{
namespace MainApplication
{
class CWindow : CApplicationCallback
{
private:
CApplication *m_CApplication;
private:
virtual void OnDraw(HDC hDC){}
virtual void OnKeyDown(WPARAM wParam, LPARAM lParam);
virtual LRESULT WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
static LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
virtual void GetWndClassEx(WNDCLASSEX & wc);
public:
HWND m_hWnd;
CWindow(void) : m_hWnd(NULL) {}
~CWindow(void) {}
virtual bool CreateEx(DWORD dwExStyle, LPCTSTR lpszClass, LPCTSTR lpszName, DWORD dwStyle,
int x, int y, int nWidth, int nHeight, HWND hParent,
HMENU hMenu, HINSTANCE hInst);
bool RegisterClass(LPCTSTR lpszClass, HINSTANCE hInst);
virtual WPARAM MessageLoop(void);
virtual BOOL ShowWindow(int nCmdShow) const
{
return ::ShowWindow(m_hWnd, nCmdShow);
}
virtual BOOL UpdateWindow(void) const
{
return ::UpdateWindow(m_hWnd);
}
};
}
}
Please help
Last edited by miteshpandey; February 3rd, 2008 at 06:27 AM.
If there is no love sun won't shine
-
February 3rd, 2008, 06:30 AM
#2
Re: C-style cast suceeds whereas static_cast reports error
I may be missing something, but why do you even need a cast? CWindow is a derived class of CApplicationCallback so you don't need a cast to assign a derived pointer to a base pointer.
Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
Supports C++ and VB out of the box, but can be configured for other languages.
-
February 3rd, 2008, 06:36 AM
#3
Re: C-style cast suceeds whereas static_cast reports error
Hi Yves,
Earlier I didn't use any casts. While not using any cast it gives the following error:
Code:
1>j:\nepwaregames\nepwaregames\nepwaregames.cpp(28) : error C2243: 'type cast' : conversion from 'NepwareGames::MainApplication::CWindow *' to 'NepwareGames::MainApplication::CApplicationCallback *' exists, but is inaccessible
The "inaccessible" thing is very puzzling. Default constructors are available for the both. Any ideas?
Last edited by miteshpandey; February 3rd, 2008 at 06:38 AM.
If there is no love sun won't shine
-
February 3rd, 2008, 06:39 AM
#4
Re: C-style cast suceeds whereas static_cast reports error
Apparently, class inheritance in C++ is private by default... can you spot the error in your code? I found this out by searching - *
-
February 3rd, 2008, 06:40 AM
#5
Re: C-style cast suceeds whereas static_cast reports error
 Originally Posted by Yves M
I may be missing something, but why do you even need a cast? CWindow is a derived class of CApplicationCallback so you don't need a cast to assign a derived pointer to a base pointer.
Yes you missed something. His inheritance is private.
Try making the inheritance public thus:
Code:
class CWindow : public CApplicationCallback
{
// etc.
};
-
February 3rd, 2008, 06:42 AM
#6
Re: C-style cast suceeds whereas static_cast reports error
Ah, yes
Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
Supports C++ and VB out of the box, but can be configured for other languages.
-
February 3rd, 2008, 06:43 AM
#7
Re: C-style cast suceeds whereas static_cast reports error
Thanks Mybowlcut.
Gosh!!! I spent half an hour with this.
If there is no love sun won't shine
-
February 3rd, 2008, 06:52 AM
#8
Re: C-style cast suceeds whereas static_cast reports error
Haha no probs.. thanks for helping me out all those times! Feels great to help someone smarter than me, even if NMTop40 was only seconds away haha.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|