Sign in to follow this  
Koen VB

Static member gives me headaches

Recommended Posts

Koen VB    133
I'm writing an application wrapper class. This is the piece of my code that causes problems: class CApplication { ... public: static bool Keys[256]; ... }; The array is initialized in the constructor. In the WndProc function, I want to use it to store keyboard data, like this: case WM_KEYDOWN: { CApplication::Keys[wParam] = true; return 0; } The program compiles, but I get a linker error: d:\programming\hypercube\application.o(.text+0xa74):application.cp: undefined reference to `CApplication::Keys' I use Bloodshed DevC++.

Share this post


Link to post
Share on other sites
JohnBolton    1372
A comment on the design:

If Keys is a static member, then you shouldn't initialize it in the constructor because it will be re-initialized everytime there is a new instance of the class (even if it is only a temporary).

If you intend for there to only be one instance of the class, then there is no reason to make it static, since there will be no other instances that will share it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this