I want to get member function pointer...

L

LinusLee

class CAnalyzer
{
// attribute
private:
public:
// behavior
private:
void NullFunc(NODE *pNode);
void TraverseNode(NODE* pNode, void (*preFunc)(NODE *node), void
(*postFunc)(NODE *node));
public:
void BuildSymbolTbl(NODE *pRootNode);
void CheckType(NODE *pRootNode);
// debug
void PrintNode(NODE *pNode);
};

I defined CAnalyzer class like this...
I want to get PrintNode and NullFunc function pointer and put this to
TraverseNode args...

I just described to get function pointer like this...

void CAnalyzer::BuildSymbolTbl(NODE *pSyntaxTree)
{
typedef void (CAnalyzer::*pPrintNode)(NODE *pNode);
typedef void (CAnalyzer::*pNullFunc)(NODE *pNode);
pPrintNode t = &CAnalyzer::printNode;
pNullFunc t2 = &CAnalyzer::NullFunc;
TraverseNode(pSyntaxTree, t, t2);
}

then... when I compile this code, compiler inform me this error msg.

error C2664: 'TraverseNode' : cannot convert parameter 2 from 'void
(__thiscall CAnalyzer::*)(union tagNodeType *)' to 'void (__cdecl
*)(union tagNodeType *)'
There is no context in which this conversion is possible

I know that member function in class is type of (__cdecl*)...
and (CAnalzer::*pPrintNode) is __thiscall*...
how can I get the correct member function pointers...
 
J

John Harrison

LinusLee said:
class CAnalyzer
{
// attribute
private:
public:
// behavior
private:
void NullFunc(NODE *pNode);
void TraverseNode(NODE* pNode, void (*preFunc)(NODE *node), void
(*postFunc)(NODE *node));
public:
void BuildSymbolTbl(NODE *pRootNode);
void CheckType(NODE *pRootNode);
// debug
void PrintNode(NODE *pNode);
};

I defined CAnalyzer class like this...
I want to get PrintNode and NullFunc function pointer and put this to
TraverseNode args...

I just described to get function pointer like this...

void CAnalyzer::BuildSymbolTbl(NODE *pSyntaxTree)
{
typedef void (CAnalyzer::*pPrintNode)(NODE *pNode);
typedef void (CAnalyzer::*pNullFunc)(NODE *pNode);
pPrintNode t = &CAnalyzer::printNode;
pNullFunc t2 = &CAnalyzer::NullFunc;
TraverseNode(pSyntaxTree, t, t2);
}

then... when I compile this code, compiler inform me this error msg.

error C2664: 'TraverseNode' : cannot convert parameter 2 from 'void
(__thiscall CAnalyzer::*)(union tagNodeType *)' to 'void (__cdecl
*)(union tagNodeType *)'
There is no context in which this conversion is possible

I know that member function in class is type of (__cdecl*)...
and (CAnalzer::*pPrintNode) is __thiscall*...
how can I get the correct member function pointers...

You are trying to convert a member function pointer to an ordinary function
pointer. That is impossible.

If you really want to use member function pointers then you are going to
have to rewrite TraverseNode. If you want to use regular function pointers
then you are going to have to rewrite everything else.

Assuming you want member function pointers then something like this

class CAnalyzer
{
....
typedef void (CAnalyser::* NODE_FUNC)(NODE*);
void TraverseNode(NODE* pNode, NODE_FUNC preFunc, NODE_FUNC postFunc);
....
};

void CAnalyzer::BuildSymbolTbl(NODE *pSyntaxTree)
{
TraverseNode(pSyntaxTree, &CAnalyzer::printNode, &CAnalyzer::NullFunc);
}

Can't help you rewrite TraverseNode because you didn't post the code for
that, but the way you call preFunc and postFunc will be different. Like this

(this->*preFunc)(someNode); // call preFunc with someNode

john
 
D

Daniel T.

class CAnalyzer
{
// attribute
private:
public:
// behavior
private:
void NullFunc(NODE *pNode);
void TraverseNode(NODE* pNode, void (*preFunc)(NODE *node), void
(*postFunc)(NODE *node));
public:
void BuildSymbolTbl(NODE *pRootNode);
void CheckType(NODE *pRootNode);
// debug
void PrintNode(NODE *pNode);
};

I defined CAnalyzer class like this...
I want to get PrintNode and NullFunc function pointer and put this to
TraverseNode args...

I just described to get function pointer like this...

void CAnalyzer::BuildSymbolTbl(NODE *pSyntaxTree)
{
typedef void (CAnalyzer::*pPrintNode)(NODE *pNode);
typedef void (CAnalyzer::*pNullFunc)(NODE *pNode);
pPrintNode t = &CAnalyzer::printNode;
pNullFunc t2 = &CAnalyzer::NullFunc;
TraverseNode(pSyntaxTree, t, t2);
}

then... when I compile this code, compiler inform me this error msg.

error C2664: 'TraverseNode' : cannot convert parameter 2 from 'void
(__thiscall CAnalyzer::*)(union tagNodeType *)' to 'void (__cdecl
*)(union tagNodeType *)'
There is no context in which this conversion is possible

I know that member function in class is type of (__cdecl*)...
and (CAnalzer::*pPrintNode) is __thiscall*...
how can I get the correct member function pointers...

Try this:

TraverseNode(pSyntaxTree, bind1st(mem_fun(&CAnalyzer::printNode), this),
bind1st(mem_fun(&CAnalyzer::NullFunc), this) );

You might have to rewrite TraverseNode as a template:

template < typename T, typename T2 >
void TraverseNode( SyntaxTree*, T, T2 );
 
D

Dave Townsend

I think you could make this a bit simpler by using Functors or
function objects instead of function pointers. I always find the
syntax for declaring function pointers a headache and have to check
it in the book before anything works. You would also
gain some benefit by having a more powerful pre-and post
function capability, since the functor object could contain its
own separate state. Just a quick resume of functors, these
are objects which have an overloaded () opertor, so they can
be called using the same syntax as a function :

Functor foo;

....in you code somewhere...
foo( pNode ); // uses an overloaded operator() to invoke useful function.

Functors are well documented in the STL standard if you would like more
info.

dave
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,459
Latest member
Vida00R129

Latest Threads

Top