Archived

This topic is now archived and is closed to further replies.

Static class member callbacks

This topic is 6143 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi, Im new to the site ( as a member anyway ), but Ive truly gone and got my self stuck I have a class like this, so that I can get everything encapsulated and run my program by just instantiating the class, but I have a problem with winproc. Seen as it needs to be static, and im calling class functions, Im trying to use the following pseudo code method: class myApp { public: funtcion 1 function 2... static myApp* m_pMyApp; static LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM ); }; inline myApp* getApp() ( return myApp::m_pMyApp; } Then in my encapsulated winproc I can just do... LRESULT CALLBACK myApp::WndProc( HWND, UINT, WPARAM, LPARAM ) { some code.... GetApp()->function 1; } Heres the thing. Im getting LNK2001 but I just cant see why. Its driving me up the wall. Ive probably made a basic mistake somewhere but if you can help Id realy appreciate it Roll on Gamedev.net Edited by - i_sys on July 27, 2001 3:44:07 PM

Share on other sites
For which symbol do you get the link error? Based on the code I assume its GetApp(). You''re calling it from inside a static member (no this pointer available there), so you have to make the GetApp() member static too.

Hope this helps,
Bjørn.

Share on other sites
"public: static class myApp* myApp::m_pMyApp" (?m_pMyApp@myApp_wrap@@2PAV1@A)

GetApp() is defined outside of the class in the header file. Also in my constructor for myApp I have

m_pMyApp = this;

Inlining is enabled, and I could swear Ive done this before and didnt have a problem. It seemed like a neat way of getting WinProc encapsulated at the time, now Im wandering..

Edited by - i_sys on July 27, 2001 4:04:48 PM

Share on other sites
I''ve done something similiar, so you may want to try this approach...

Create your window procedure function, without the static, but make it public.

Create a class variable globally and call the function from a global window proc...

class CMyClass
{
public:
WinMain( ... );
WndProc( ... );

private:
...
};

CMyClass MyClass;

WinMain( ... )
{
MyClass.WinMain( ... );
}

WndProc( ... )
{
MyClass.WndProc( ... );
}

G''luck,
-Alamar

Share on other sites
i_sys,

Your linker error is caused by the fact the static class member 'm_pMyApp' has a type, but no associated storage (a declaration but no definition?)

Anyway whenever you use a static member variable file you need a definition of the following form

static typename classname::membername [ = initialization_value ]

This definition must only be compiled once (to avoid redefintion of symbol errors) so should be usually be included in a source file.

So give the following line a try

static myApp* myApp::m_pMyApp = NULL;

(The NULL bit is optional, but IMO good programming practice)

Hope this helps

Tom.

Edited by - TomH on July 30, 2001 9:23:28 AM

• 9
• 9
• 13
• 41
• 15