Главная страница | назад





Article #16805: Why can't I use a member function of an object to create a thread

 Question and Answer Database
FAQ1805C.txt Why can't I use a member function of an object to create a thread
Category :Windows API
Platform :All
Product :C++Builder 1.x
Question:
I'm trying to spin a thread, and I want to use a member
function as the thread procedure. But I can't get
the compiler to accept it. Why not?
Answer:
C++ does not allow this behavior.
Class methods are stored in memory in exactly (1) location.
The way that a class method accesses other members of a
class is via a pointer which is silently passed as the
first argument to the method; windows knows nothing about
this pointer, and is unable to pass it.
The compiler will not allow you to call a non-static member
function without an object because it does not know what
to store in the this pointer; if it did, you would still
not be happy with the result, because windows would NOT
pass a this pointer as the first argument.
To get around this, you can:
(a) use a static method.
(b) have a global c-style function which calls the method
of a particular object by invoking it explicitly, eg.
int foo() {
object.method()
}
and then pass that to the thread-invocation function.
7/2/98 10:32:32 AM

Last Modified: 01-SEP-99