Non-Static Callbacks
-
I read this about using a wrapper for Non-Static Callbacks within a class. (http://www.codeproject.com/cpp/SetTimer\_\_non-static.asp) Its brilliant. I can't believe I never thought of it. Anyway, would it be possible to use a Non-Static callback with threads? I want to run another thread within my class using CreateThread, but I want the thread to be able to access the members of my class. Is it possible?
-
I read this about using a wrapper for Non-Static Callbacks within a class. (http://www.codeproject.com/cpp/SetTimer\_\_non-static.asp) Its brilliant. I can't believe I never thought of it. Anyway, would it be possible to use a Non-Static callback with threads? I want to run another thread within my class using CreateThread, but I want the thread to be able to access the members of my class. Is it possible?
I believe you can use a similiar approach such as: Let's say Class A is the class that you want to access from within the thread. Then you can do something like: in A::something() : (if you're using VC++) AfxBeginThread(fnThread, this); in fnThread(LPVOID param) : A* pObj = static_cast(param); then you can access A's methods like: pObj->aMethod(); of course, provided you have the required access level for it.
-
I believe you can use a similiar approach such as: Let's say Class A is the class that you want to access from within the thread. Then you can do something like: in A::something() : (if you're using VC++) AfxBeginThread(fnThread, this); in fnThread(LPVOID param) : A* pObj = static_cast(param); then you can access A's methods like: pObj->aMethod(); of course, provided you have the required access level for it.
Right now I decided to use the multimedia timers to call my drawing routine, and the callback doesnt support and void user-defined params. So I figured out all I really need to do is name a pointer of type of my class static and put it in the class declaration like so: static CScroller * ClassPtr; So now my static wrapper callback can access it and use it to call the non-static member function that does all the drawing work. What a nice little hack. I love it. Anyway as I was reading that article i mentioned earlier from the code project, I noticed how just having "static CScroller * ClassPtr;" in my class declaration would cause an undefined external object linking error. The article mentioned putting "CScroller * CScroller::ClassPtr;" in my class implementaion. That fixed the error. What I want to know is how does doing that fix it and why does it get that linking error if I don't put it?
-
Right now I decided to use the multimedia timers to call my drawing routine, and the callback doesnt support and void user-defined params. So I figured out all I really need to do is name a pointer of type of my class static and put it in the class declaration like so: static CScroller * ClassPtr; So now my static wrapper callback can access it and use it to call the non-static member function that does all the drawing work. What a nice little hack. I love it. Anyway as I was reading that article i mentioned earlier from the code project, I noticed how just having "static CScroller * ClassPtr;" in my class declaration would cause an undefined external object linking error. The article mentioned putting "CScroller * CScroller::ClassPtr;" in my class implementaion. That fixed the error. What I want to know is how does doing that fix it and why does it get that linking error if I don't put it?
Hi All static members of a class must be defined somewhere. This is necessary since unlike normal member variables, which are created when a class is instantiated, static member variables exist even when there is no instance of the class. For example, if you have something like: in A.h: class A { static int _anInt; }; the you define it as (in A.cpp): int A::_anInt; The definition is usually put on top of your class implementation file, and it is also useful for setting a default value for your static member (i.e. if you want to override the default 0 value), e.g: in A.cpp: int A::_anInt(10) //set _anInt to 10