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
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.
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?
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? :D I found this out by searching - *
Re: C-style cast suceeds whereas static_cast reports error
Quote:
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.
};
Re: C-style cast suceeds whereas static_cast reports error
Re: C-style cast suceeds whereas static_cast reports error
Thanks Mybowlcut.
Gosh!!! I spent half an hour with this. :thumb:
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. :p