Re: Member function pointer to member function of another class
On Apr 2, 10:14 am, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
Praetorian wrote:
Hi,
I'm having huge syntax problem dealing withfunctionpointers. Here's
what I'm doing:
I have 2 classes, ClassA and ClassB. ClassA owns an instance of
ClassB. What I want to do is pass afunctionpointerto amember
functionof ClassB as a parameter to amemberfunctionof ClassA. I
don't want ClassA'smemberfunctionto call ClassB'sfunctiondirectly
because ClassA can be used in 2 ways, one of which is a case in which
it does not create an instance of ClassB; hence by passing afunction
pointerI can handle that case easily.
This is the code I have:
classClassB {
public:
BOOL GetFlagX(void) { return m_bFlagX;};
private:
BOOL m_bFlagX;
};
classClassA {
public:
typedef BOOL (ClassA::*FPAbortFlag)(void);
FPAbortFlag m_fpAbort;
protected:
ClassB *m_pClassB;
BOOL ClassAMemFcn(FPAbortFlag fpAbort);
};
In this case ClassA has new'd an instance of ClassB. What I want to do
is have ClassA'sfunctionpointer'm_fpAbort' point to ClassB's
'GetFlagX'function. Can't seem to figure out the syntax for this.
I did get the following to compile but it wasn't pointing to the
memberfunctionassociated with ClassA's instance of ClassB:
m_fpAbort = (FPAbortFlag)(&(ClassB::GetFlagX));
There should be no need for the cast or for the parentheses.
m_fpAbort = &ClassB::GetFlagX;
If this doesn't work for you, post the code so we can take a closer
look (even copy-and-paste it locally and compile). See FAQ 5.8.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Victor,
That didn't work; I get the following compiler error:
error C2440: '=' : cannot convert from 'BOOL (__thiscall
CBootLoaderDlg::* )(void)' to 'CFlashDownload::FPFlashAbort'
Types pointed to are unrelated; conversion requires
reinterpret_cast, C-style cast or function-style cast
I tried the reinterpret_cast suggestion as follows:
m_fpAbortFlash = reinterpret_cast <FPFlashAbort>
&CBootLoaderDlg::GetDlgCancelledFlag;
That compiles but the function never returns true; even though I can
breakpoint and verify that the flag is set. It looks like it isn't
pointing to the correct instance of CBootLoaderDlg.
Here are snippets of the actual code:
In FlashDownload.h
#include "BootLoader.h"
#include "BootLoaderDlg.h"
class CFlashDownload
{
public:
/* Constructor/Destructor */
CFlashDownload();
~CFlashDownload();
void FlashDownloadWithUI(void);
private:
CBootLoaderDlg *m_pBootDlg;
typedef BOOL (CFlashDownload::*FPFlashAbort)();
FPFlashAbort m_fpAbortFlash;
BOOL Flash2812InternalRam(FPFlashAbort fpAbortFlash);
};
In BootloaderDlg.h
class CBootLoaderDlg : public CDialog
{
DECLARE_DYNAMIC(CBootLoaderDlg)
public:
BOOL GetDlgCancelledFlag(void) { return m_bDlgCancelled; };
private:
BOOL m_bDlgCancelled;
};
In FlashDownload.cpp
void CFlashDownload::FlashDownloadWithUI(void)
{
if( m_pBootDlg != NULL ) {
AfxMessageBox( _T("Recursive call to FlashDownloadWithUI()
detected") );
AfxThrowUserException();
}
m_pBootDlg = new CBootLoaderDlg();
m_pBootDlg->Create( IDD_MAIN_DLG, AfxGetMainWnd() );
m_fpAbortFlash = &CBootLoaderDlg::GetDlgCancelledFlag;
/* Call flash download function */
Flash2812InternalRam(m_fpAbortFlash);
m_pBootDlg->DestroyWindow();
delete m_pBootDlg;
}
CBootLoaderDlg sets m_bDlgCancelled when the dialog is cancelled
(within its OnCancel()) method).
Thanks for your help, I really appreciate it. Sorry for this silly
question but you mentioned I should look at section 5.8 of the C++ FAQ
but how do I get to the FAQ?
Ashish.