Static class member callbacks

Started by
3 comments, last by i_sys 22 years, 8 months ago
Hi, I`m new to the site ( as a member anyway ), but I`ve 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 i`m calling class functions, I`m 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; } Here`s the thing. I`m 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
Advertisement
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.
We are boki. The rest is known.
"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 I`ve done this before and didnt have a problem. It seemed like a neat way of getting WinProc encapsulated at the time, now I`m wandering..

Edited by - i_sys on July 27, 2001 4:04:48 PM
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
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

This topic is closed to new replies.

Advertisement