CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2003
    Location
    kathmandu, nepal
    Posts
    1,570

    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

  2. #2
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588

    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.

  3. #3
    Join Date
    Apr 2003
    Location
    kathmandu, nepal
    Posts
    1,570

    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

  4. #4
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    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 - *
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  5. #5
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    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.
    };

  6. #6
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588

    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.

  7. #7
    Join Date
    Apr 2003
    Location
    kathmandu, nepal
    Posts
    1,570

    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

  8. #8
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    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.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured