Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Non-Static Callbacks

Non-Static Callbacks

Scheduled Pinned Locked Moved C / C++ / MFC
c++comquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bmw5002
    wrote on last edited by
    #1

    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 1 Reply Last reply
    0
    • B bmw5002

      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 Offline
      I Offline
      Indrawati
      wrote on last edited by
      #2

      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.

      B 1 Reply Last reply
      0
      • I Indrawati

        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.

        B Offline
        B Offline
        bmw5002
        wrote on last edited by
        #3

        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?

        I 1 Reply Last reply
        0
        • B bmw5002

          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?

          I Offline
          I Offline
          Indrawati
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups