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

  • SEARCH
  • Home
  • 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 7557175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:03:48+00:00 2026-05-30T12:03:48+00:00

Like the title. As a new programmer,It is very important to know the mechanism

  • 0

Like the title.
As a new programmer,It is very important to know the mechanism for my project, and what is the difference of using them. Can any guy give me a answer,thank you!

  • 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-30T12:03:50+00:00Added an answer on May 30, 2026 at 12:03 pm

    A virtual function is one mechanism to provide a callback, but it is not the only mechanism.

    The generality of a callback is that you pass in, as a parameter, a method, or an object that invokes a method, that will be called by the function you pass it to, or will be stored by that function to be called later when an event happens.

    One way to implement such a mechanism is to pass in a pointer or reference to an object that derives from a base class and implements a virtual function.

    Another way is to pass in a function pointer.

    Yet another is to pass in an object that has a function pointer, which is what boost functions are – the constructors to boost function (often boost::bind) cleverly construct such an object for you.

    In C++11 you will be able to construct an unnamed function on the fly to be passed as a callback. This is usually referred to as a lambda.

    A bit more about the mechanism of using a virtual function as a callback. It can look like quite a neat thing to do:

    class Handler
    {
      public:
       virtual ~Handler() {}
       virtual void process( std::string const& message ) = 0;
    };
    
    void getMessage( Handler& handler )
    {
        std::string str = getAMessage();
        handler.process( str );
    }
    

    Even tidier is to construct Handler this way:

    class Handler
    {
      public:
        virtual ~Handler() {}
        void process( std::string const& message ) 
        {
           // can log or put in a breakpoint here on debugging
            doProcess( message );
        }
    
       protected: // or private
           virtual void doProcess( std::string const& message ) = 0;
    };
    

    The advantage here is that it is easy to debug into.

    It becomes more complex if the lifetime of the Handler object needs to be managed, i.e. the callback happens asynchronously. Then you have to use a shared_ptr<Handler> so that both sides hold a reference, and it will be managed.

    One huge advantage you will find by doing this, over a regular callback function, is that the object can hold extra information. The callback itself can only take a fixed number of parameters, and in a callback one will often use a void* and have to cast it to something to get back the extra information. Using the class allows you to hold that information in member variables, as well as allowing the class to retain state after its method was invoked.

    The overhead of using this though is that you have to create such abstract classes and then derive classes from it, and you might find yourself doing this rather a lot.

    This is where boost function and bind becomes very useful. What you really want is a function with a specific signature to be called, and not to have to create extra classes to handle this callback. It enables you therefore to program this situation much more quickly.

    However, it unfortunately does have a downside too. boost functions are far more difficult to be able to debug into. Whilst you do not have to be careful about managing the lifetime of the boost function itself, you do have to be careful about the parameters you pass into it. It will just blindly store them the way you pass them. If you pass in a reference to local variable as a reference-holder (boost::ref) it will indeed store this reference and if it happens to go out of scope before the function is invoked, you may have a hard time locating this bug.

    In spite of this though, I would suggest making use of the library feature (note there is also std::function and std::bind, although when lambdas become standard among compilers, there will be far less requirement for them).

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

Sidebar

Related Questions

Possible Duplicate: What does new() mean? Like in title. I wonder what this syntax
I have an action like so: @Html.ActionLink(dinner.Title, Details, new { id=dinner.DinnerID }) and a
my json file looks like this [{id:1,category:New Category1,title:New Product2,description:Please type a description1,imgurl:http:\/\/s.wordpress.org\/about\/images\/wordpress-logo-notext-bg.png,spectitle:Specification,specvalue:Value},{id:2,category:Mobile,title:Samsung,description:Please type a
var gallery = new TGallery([[{image:{updated_at:2010-03-20T00:00:00Z,title:dfgdfg,spam_reports:0,by_profile_id:1,comment_count:0,id:1,description:htt,on_type:profile,adult_reports:0,on_id:1,created_at:2010-03-20T00:00:00Z}]]); Then in the TGallery class I would like to
I have a text like: Title A some description on a few lines, there
Searching from google.com, like www.abc.com Search Result Rank the pages like Title..... Description... www.abc.com
Like the title says I'm having some trouble printing out symbol codes and their
Like the title says, I'm wondering if the use of WebClient is the proper
Like the title says, PHP is really confusing me on a simple if comparison
Like the title says, I'm trying to determine if data is a standard attribute

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.