Re: Creating thread from a class object
David Lowndes wrote:
Can you elaborate on that?
Briefly, my train of thought was ... a static method needs static
member variables (unless you add the complexity of passing an instance
pointer which will further confuse many) so folks will immediately
start making member variables static - "to make it work" (i.e.
compile) - which will undoubtedly be the wrong thing to do. Ultimately
it's just my gut feeling of what might happen.
In my opinion, the kind of developers you are referring to usually don't go
around creating threads, or use static member functions for that matter.
The fact that the original question posed on this thread exists (and
the same thing crops up frequently) is proof that many developers are
confused by the situation.
Dave:
Yes, but the orginal question is invariably really (when translated out
of the confused state of the poster):
"How do I use a non-static class method as the thread function?"
The answer is to "use a global or class-static thread function, and
access the non-static method by casting the LPVOID parameter". So the
"instance parameter" is a required feature of the answer, not an
optional extra.
Although the question is sometimes phrased as
"Why do I get compile errors when I use a non-static class method as the
thread function?"
the poster will not be satisfied with the answer "You must use a lobal
or class-static thread function" because they really did want a
non-static class method.
For me, using a class-static rather than global function is a simple
matter of encapsulation, though it does (as noted) have the advantage of
allowing access to private parts of the class (so that the "non-static
thread function" can be made private).
David Wilkinson