473,320 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 3076

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
15
by: Albert | last post by:
Hi, I need to pass a pointer-to-member-function as a parameter to a function which takes pointer-to-function as an argument. Is there any way to do it besides overloading the function? Here...
7
by: jon wayne | last post by:
Hi I'm a little confused here about the lifetime of a static pointer to member function, Say, I declare,define & initialize a static ptr to mem function in the header file of a class(the class...
4
by: Agoston Bejo | last post by:
Hi, I would like to do something like this: struct A { int f(float f); }; .... int g(int(*f1)(float)) { return f1(6.5); }
1
by: Assertor | last post by:
Hi All, Except function pointer to a static member funciton of a class (or a function). As the following code, I could pointer a non-static member function of a class. And "Equal" was...
7
by: Bala L | last post by:
I have a class with a private array data member 'm_array'. I have written a public function, called 'fileRead', to read values into the array from a file. I just noticed that I have declared this...
12
by: WaterWalk | last post by:
Hello. I am rather confused by the type of a pointer to class data member. Many c++ texts say that pointer to data member has a special syntax. For example the following class: class MyClass {...
7
by: WaterWalk | last post by:
Hello. I thought I understood member function pointers, but in fact I don't. Consider the following example: class Base { public: virtual ~Base() {} }; class Derived : public Base {
4
by: Immortal_Nephi | last post by:
I had a lot of research to see how function pointer works. Sometimes, programmers choose switch keyword and function in each case block can be called. Sometimes, they choose ordinary function...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.