Article View: borland.public.cppbuilder.nativeapi
Article #8457Re: Threads, Functions and Classes
From: Andrue Cope [TeamB]
Date: Tue, 26 Oct 2004 11:47
Date: Tue, 26 Oct 2004 11:47
59 lines
2845 bytes
2845 bytes
Alistair wrote:
> I realise its because I have defined the function within my class,
> but I really need it to be defined within my class!!
The problem is that non-static methods must have access to a 'this'
pointer. C++ passes that invisibly to every such method. Without doing
that the method has no way of knowing which object it is being called
on. There is only ever one copy of the code for every method. Only
object data is replicated for every object and the invisible argument
is used to identify this data.
One solution is to create an instance of the class and reference that
from within the function. Pseudo code example:
TOMyClass * objectPtr;
DWORD callBackFunction( void * ObectPtr )
{
objectPtr->DoSomething( ObjectPtr );
}
You will have to remember to set up the object pointer (objectPtr in
this example) before passing the function pointer. Note that this also
doesn't lend itself to calling back multiple objects at the same time.
By that I mean that if you have ten objects at any one time and want
any one of them to be called back as needed this won't work.
I note however that the caller is passing a 'void *' to you. That looks
to me like it's intended to be used as a handle. IOW it is there to
provide state information to identify the callback and the purpose
behind it.
If that is the case the solution is to make the 'void *' be a pointer
to an object so that you can do:
DWORD callBackFunction( void * ObjectPtr )
{
reinterpret_cast<TOMyClass *>( ObjectPtr )->DoSomething();
}
If additional information is needed then an intermediate structure
could be used:
struct TCallBackInfo
{
void * SomeInfo;
TOMyClass * ObjectPtr;
};
DWORD callBackFunction( void * ObjectPtr )
{
TCallBackInfo * cbi=reinterpret_cast<TCallBackInfo *>( ObjectPtr );
cbi->ObjectPtr->DoSomething( cbi->SomeInfo );
}
--
Andrue Cope [TeamB]
[Bicester, Uk]
http://info.borland.com/newsgroups/guide.html
Technical Details
Message-ID:
<417e3939@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!border1.nntp.dca.giganews.com!nntp.giganews.com!peer01.cox.net!cox.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailReferences:
<417e2ee6@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,fc124e2275bfd1e9 X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Andrue Cope [TeamB]" <no.spam@not.a.valid.address> Subject: Re: Threads, Functions and Classes Newsgroups: borland.public.cppbuilder.nativeapi References: <417e2ee6@newsgroups.borland.com> User-Agent: XanaNews/1.16.4.6 MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 NNTP-Posting-Host: 217.207.223.34 Message-ID: <417e3939@newsgroups.borland.com> Date: 26 Oct 2004 04:47:05 -0700 X-Trace: newsgroups.borland.com 1098791225 217.207.223.34 (26 Oct 2004 04:47:05 -0700) Lines: 59 X-Authenticated-User: acope Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!border1.nntp.dca.giganews.com!nntp.giganews.com!peer01.cox.net!cox.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail