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 9148453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:16:39+00:00 2026-06-17T11:16:39+00:00

In Java, when I need a callback function, I have to implement an anonymous

  • 0

In Java, when I need a callback function, I have to implement an anonymous class. Inside the anonymous class, I can access the outside variables if they’re final.

Now I’m doing the same thing in C++. I understand that C++ lambda works better but sometimes I need to pass in many functions where with anonymous classes, I only need to pass in one instance.

I tried the following example. It works with GCC 4.3.4.

class IA {
public:
  virtual int f(int x) = 0;  
};

int main() {
    class : public IA {
        int f(int x) { return x + 1; }
    } a;
    doFancyWork(&a);
    return 0;
}

Is it possible to capture the outside variables like this?

int main() {
    int y = 100; // mark y as final if possible
    class : public IA {
        int f(int x) { return x + y; }
    } a;
    return 0;
}

UPDATE:

The second example won’t compile. The errors are here,

prog.cpp: In member function ‘virtual int main()::<anonymous class>::f(int)’:
prog.cpp:9: error: use of ‘auto’ variable from containing function
prog.cpp:7: error:   ‘int y’ declared here
prog.cpp: In function ‘int main()’:
prog.cpp:7: warning: unused variable ‘y’

UPDATE:

I just realized a few more problems in doing this:

  • I cannot write a constructor because the class doesn’t have a name
  • initializer list doesn’t allow inheritance.
  • any change to make it compile makes the code unreadable.

I think I have to move away from anonymous classes.

  • 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-06-17T11:16:41+00:00Added an answer on June 17, 2026 at 11:16 am

    There is no way to automatically capture those variables, but you can use an alternative approach. This is if you want to capture by reference:

    int main() {
        int y = 100; // mark y as final if possible
        class IB : public IA {
        public:
          IB(int& y) : _y(y) {}
          int f(int x) { return x + _y; }
        private:
          int& _y;
        } a (y);
        return 0;
    }
    

    If you want to capture by value, just change int& into int.

    Anyway, you may consider using a tuple of lambdas as a “multi-callback” object if that is what bothers you about individual lambdas. You would still have everything packed in one object and capturing would be done for free.

    Just as an example:

    auto callbacks = make_tuple(
        [] (int x) { cout << x << endl; },
        [&] () { cout << y << endl; }, // y is captured by reference
        [=] (int x) { cout << x + y << endl; }, // y is captured by value
        // other lambdas here, if you want...
        );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to implement callback function in Java using interface. I have wrote the
Question:- Does java client need to worry about multiple servers ? Meaning:- I have
I need a Java function that returns the results of a SQL SELECT query
I need a Java class to submit tickets to BMC Remedy's Helpdesk product. Wondering
I need a java script function that converts the document object of the current
Are there any standard generic callback or function/method types in Java, like System.Action<T> or
A java class does something like the following public class Foo { private final
I have a Java application that I need to integrate our existing PHP website
I have written the following code: import java.util.Calendar; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; class Voter
I am developing an application where I need to have constant Camera running inside

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.