Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1079313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:51:01+00:00 2026-05-16T21:51:01+00:00

I have created a very simple Tree implementation, and I would like to be

  • 0

I have created a very simple Tree implementation, and I would like to be able to pass a function object to my traverse() function. e.g.

template<class T>
class MyTree
{
public:

    void traverse(MyFunction f) {
        traverse(root, f);
    }
private:
      MyTreeNode<T>* root;
      void traverse(MyTreeNode<T>*, MyFunction f);
};

The thing is, how do I declare f if I want to pass in some parameters as well as the node in question? ( pointers to other structs ). Alternatively, can anyone point me in the direction of some tutorials?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T21:51:02+00:00Added an answer on May 16, 2026 at 9:51 pm

    There are different things that you can do at this stage.

    The old style C type solution: you can pass a function pointer:

    template <typename T>
    void MyTree<T>::traverse( void (*function)( MyTreeNode<T>*, int ) );
    

    That will take as argument a function pointer (free function) that takes a pointer to a MyTreeNode<T> object as first argument and an integer as second argument. And you can typedef the function type to ease usage:

    template <typename T>
    class MyTree {
    public:
       typedef void func( MyTreenode<T>*, int );     // typedef the function type
       typedef void (*pfunc)( MyTreenode<T>*, int ); // or the function pointer
       void traverse( func* function );
       void traverse2( pfunc function );
    };
    

    You can take any callable thing as argument with the help of a template:

    template <typename T>
    class MyTree {
    public:
       template <typename Functor> 
       void traverse( Functor f );
    };
    

    Which in the simplest approach is simple if you leave the argument types free, but the error messages if you try to pass a callable entity that takes a different set of arguments may not be so simple to parse. Internally you can use f as if it were a function, but externally can be either a function or function objects, including the result of std::bind (c++0x) or boost::bind

    You can go one step forward and actually enforce in the signature of the traverse function the arguments you are going to use by using std::function (again, c++0x) or boost::function:

    template <typename T>
    class MyTree {
    public:
       void traverse( std::function< void (MyTreeNode<T>*, int )> f );
    };
    

    The advantage of this solution is that it is clear how you are going to use the passed in parameter (as in the function pointer approach): you will call f( x, y ) where x is a MyTreeNode<T>* and y is an integer. At the same time you provide the genericity of the templated solution by means of std::bind (c++0x) or boost::bind, that can be used to adapt different entities to the function<...> argument, including free functions, function objects, member functions…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have very simple problem. I need to create model, that represent element of
Have created a c++ implementation of the Hough transform for detecting lines in images.
I have created a template for Visual Studio 2008 and it currently shows up
I have created a C# class file by using a XSD-file as an input.
I have created a custom dialog for Visual Studio Setup Project using the steps
I have created a PHP-script to update a web server that is live inside
I have created a UserControl that has a ListView in it. The ListView is
I have created a few small flash widgets that stream .mp3 audio from an
i have created a workflow activity that do give the item creater of a
I have created a foreign key (in SQL Server) by: alter table company add

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.