Thread: How can I reference to non static variable inside a static function?

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    58

    How can I reference to non static variable inside a static function?

    Hi everyone,
    if I run a thread, how can I reference to the non static variable outside the function?

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    "Expose" a pointer to that variable.

    gg

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    58
    example ? what do you mean by expose ?

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Pass the variable to the static function.

    When you create a thread, you can usually pass a generic void* pointer to the thread function; so just pass a pointer to your object (maybe by passing the this pointer)...

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by cpjust View Post
    Pass the variable to the static function.

    When you create a thread, you can usually pass a generic void* pointer to the thread function; so just pass a pointer to your object (maybe by passing the this pointer)...
    Yes and the actual thread function that receives this pointer should simply be a tiny stub function that calls a member function on that pointer to do the real work. That way you don't end up with a function that has pThis->doSomething(); etc all through it as if it were an attempt at OOP using C.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The problem is that a static function is not inside the space of the class, so to speak. It does not have a valid this pointer, you might say. Thus the problem is that you cannot access non-static data inside a static class function.
    The solution, as everyone says, would be to take a pointer to the actual class instance and fetch the variables from it or call a non-static member function on it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    There is a simple way to do this. Your static function does nothing but call a non-static function to do whatever needs to be done. The non static function can then access whatever it needs to and returns to the static function. The static function returns to caller and everyone is happy.

    If you have a great number of non-statics you must access this is the route I would choose.

    Essentially:
    Yes and the actual thread function that receives this pointer should simply be a tiny stub function that calls a member function on that pointer to do the real work. That way you don't end up with a function that has pThis->doSomething(); etc all through it as if it were an attempt at OOP using C.
    Which has already been said.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 01-19-2009, 07:42 PM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Problem with OpenGL tutorial
    By 2Biaz in forum Windows Programming
    Replies: 18
    Last Post: 09-16-2004, 11:02 AM
  5. Problem with Template Function and overloaded equality operator
    By silk.odyssey in forum C++ Programming
    Replies: 7
    Last Post: 06-08-2004, 04:30 AM