static const int& as a class data member

closed account (D80DSL3A)
How can I initialize a static class member which is a const int& ?

I'm creating a class for drag and drop objects to be used in a game.
I'd like the objects to always "know" the location of the mouse cursor so I don't have to pass the mouse coordinate as a parameter in function calls.

I have tried this:
1
2
3
4
5
class dragDrop
{
    static const int& mseX;
    ....
}

Then in main.cpp I declare global variables and attempt to initialize the static reference from above:
in main.cpp:
1
2
int mseX = 0;
const int& dragDrop::mseX = mseX;

This compiles ok, but the program crashes the 1st time I click with the mouse.
( I get a memory access error ). The call to dragDrop::hit() (the hit-test function) triggers the crash. This is one place where the int& mseX is used.

I have worked around this by using a static int* instead and this works, but I'd like to be able to use the reference method.

How do I make it work? Am I initializing the reference incorrectly?
I think the problem is the const keyword. I am guessing (because I rarely mess with C++ this deeply) that the compiler feels free to optimize away any references to a constant and instead place the actual constant value known at compilation type, which I imagine is the zero assigned to mseX. If this reasoning is correct, removing const should work. But then again, if you do that, what's the point of having a reference? It would be simple to just have a static int.
If that is the case, maybe volatile could help.
Replace

const int& dragDrop::mseX = mseX;
with

const int& dragDrop::mseX = ::mseX;
or use different names for the int and for the reference.

closed account (D80DSL3A)
Thank you all for the tips.
@Cubbi. It turned out to be as you said.
I didn't think there would be a scope confusion issue but there is!
Changing to: const int& dragDrop::mseX = ::mseX; worked.
I then changed the member name to r_mseX and now:
const int& dragDrop::r_mseX = mseX;
works fine too.

@webJose. The const keyword seems to be ok to use.
I am used to using const&'s in the context of class member function arguments such as:
obj(const obj& r_obj);// copy ctor
and
obj operator+(const obj& r_obj)const;// operator overloading
where it allows access to the member values.
I'm already doing as you suggested by using a static int in my button class.
The problem is that this needs to be updated when the mouse moves. Now when the mouse moves I would have:
1
2
3
4
5
case WM_MOUSEMOVE:
    button::mseX = LOWORD (lParam);			
    button::mseY = HIWORD (lParam);
    dragDrop::mseX = LOWORD (lParam);			
    dragDrop::mseY = HIWORD (lParam);

I want to limit this before it gets out of hand! I'd like to update one set of global variables and pass out references to them.

@ne555. I looked up the volatile keyword since I've heard of it but never used it.
MSDN wrote:
The volatile keyword is a type qualifier used to declare that an object can be modified in the program by something such as the operating system, the hardware, or a concurrently executing thread.

I didn't see how any of those conditions applied. Thanks though!
Topic archived. No new replies allowed.