how to passing member function as parameter

Discussion in 'C++' started by sun, Mar 11, 2008.

  1. sun

    sun Guest

    I have a tree structure defined as Nodes with parent and children. a
    tree class holds traverse function and other functions.

    When traversing the tree with its nodes, I 'd pass one of the function
    in node, func1() as parameters, is it possible? if so, how to define that?

    class Node{
    .....
    public:
    func1();//do sth
    }
    class Tree{
    Node* root;
    ..
    public:
    traverse(Node,func1);
    }

    Thanks!
     
    sun, Mar 11, 2008
    #1
    1. Advertising

  2. On Mar 11, 3:08 am, sun <> wrote:
    > I have a tree structure defined as Nodes with parent and children. a
    > tree class holds traverse function and other functions.
    >
    > When traversing the tree with its nodes, I 'd pass one of the function
    > in node, func1() as parameters, is it possible? if so, how to define that?
    >
    > class Node{
    > ....
    > public:
    > func1();//do sth}
    >
    > class Tree{
    > Node* root;
    > ..
    > public:
    > traverse(Node,func1);
    >
    > }
    >
    > Thanks!

    Yes by using to pointer to member functions and here is a compilable
    example.

    #include <iostream>

    class Node{

    public:
    void func1(){std::cout<<"called";}//do sth
    };
    class Tree{
    public:
    Node* root;
    void traverse(Node* node,void (Node::*funcptr)()){
    (node->*funcptr)();
    }

    };
    int main()
    {
    Node* nodeptr = new Node;
    Tree tree;
    tree.traverse(nodeptr,&Node::func1);
    }

    also check out http://www.parashift.com/c -faq/pointers-to-members.html
     
    hurcan solter, Mar 11, 2008
    #2
    1. Advertising

  3. sun

    Sun Guest

    Thank you very much, very much indeed!
    It would take me days of reading and searching without your tips.

    My code is working now.
    Nice day,

    Sun

    "hurcan solter" <> wrote in message
    news:...
    > On Mar 11, 3:08 am, sun <> wrote:
    >> I have a tree structure defined as Nodes with parent and children. a
    >> tree class holds traverse function and other functions.
    >>
    >> When traversing the tree with its nodes, I 'd pass one of the function
    >> in node, func1() as parameters, is it possible? if so, how to define
    >> that?
    >>
    >> class Node{
    >> ....
    >> public:
    >> func1();//do sth}
    >>
    >> class Tree{
    >> Node* root;
    >> ..
    >> public:
    >> traverse(Node,func1);
    >>
    >> }
    >>
    >> Thanks!

    > Yes by using to pointer to member functions and here is a compilable
    > example.
    >
    > #include <iostream>
    >
    > class Node{
    >
    > public:
    > void func1(){std::cout<<"called";}//do sth
    > };
    > class Tree{
    > public:
    > Node* root;
    > void traverse(Node* node,void (Node::*funcptr)()){
    > (node->*funcptr)();
    > }
    >
    > };
    > int main()
    > {
    > Node* nodeptr = new Node;
    > Tree tree;
    > tree.traverse(nodeptr,&Node::func1);
    > }
    >
    > also check out http://www.parashift.com/c -faq/pointers-to-members.html
     
    Sun, Mar 11, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Newsgroup - Ann
    Replies:
    5
    Views:
    662
    John Carson
    Jul 30, 2003
  2. James Vanns
    Replies:
    7
    Views:
    7,169
    Evan Carew
    Jan 21, 2004
  3. Replies:
    8
    Views:
    572
    PJP of NYC
    May 24, 2005
  4. Azdo
    Replies:
    2
    Views:
    453
  5. Mister B
    Replies:
    8
    Views:
    600
    Nick Keighley
    Aug 26, 2010
Loading...

Share This Page