By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 884 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

I want to get member function pointer...

P: n/a
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...
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"LinusLee" <lo****@hanmail.net> wrote in message
news:6c**************************@posting.google.c om...
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
Jul 22 '05 #2

P: n/a
lo****@hanmail.net (LinusLee) wrote:
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 );
Jul 22 '05 #3

P: n/a
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
"LinusLee" <lo****@hanmail.net> wrote in message
news:6c**************************@posting.google.c om...
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...

Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.