469,271 Members | 1,442 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

I want to get member function pointer...

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
3 2836

"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
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
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.

Similar topics

37 posts views Thread by Ben | last post: by
15 posts views Thread by Albert | last post: by
7 posts views Thread by jon wayne | last post: by
4 posts views Thread by Agoston Bejo | last post: by
7 posts views Thread by Bala L | last post: by
12 posts views Thread by WaterWalk | last post: by
7 posts views Thread by WaterWalk | last post: by
4 posts views Thread by Immortal_Nephi | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.