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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:30:47+00:00 2026-06-08T00:30:47+00:00

In Java, if I have an interface, I can do something like this: blah.setOnClickListner(new

  • 0

In Java, if I have an interface, I can do something like this:

blah.setOnClickListner(new OnClickListner() {
     public void clicked() { // do something }
}

Can I do something similar in C++?

  • 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-08T00:30:49+00:00Added an answer on June 8, 2026 at 12:30 am

    C++ supports local classes. The syntax is the same as for any class type, except that member functions and static member variables must be defined inside the class body, not declared and defined later (there’s no way to name the member from outside the enclosing function).

    Beginning in C++11, they can be used as template type parameters, which makes them much much more useful.

    It would look something like:

    void Parent::func( EventProducer* blah )
    {
        struct LocalListener : OnClickListener
        {
            virtual void clicked() { ... }
        };
        struct FancyLocalListener : OnClickListener
        {
            Parent* p;
            FancyLocalListener(Parent* p) : p(p) {} // but use better variable names, please ;)
            virtual void clicked() { p->func2(); }
        };
    
        blah->addOnClickListener(new LocalListener());
        blah->addOnClickListener(new FancyLocalListener(this));
    }
    

    (but watch out for leaks)

    C++ tends not to use interfaces for this, though. A better design is to accept a functor for the listener, which in C++11 allows the use of lambdas (the compiler creates the local class for you).

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

Sidebar

Related Questions

I have something like this going on in my Java program: void f(Object o)
When I compile something like this: public class MyClass { void myMethod(String name, String
So I have this interface public interface EventHandler<E extends EventObject> { public void handleEvent(E
I am learning Java and just found that the Interface can have fields, which
Suppose I have these interfaces: public interface I1 { void foo(); } public interface
I have code that looks like follows: public interface BaseDAO{ // marker interface }
I have the following Java interfaces, public interface JavascriptInterface { public MyIterator getItems(); }
I'm trying to do something like this, but Java won't let me. I suspect
I have an java interface public interface IPerson { Person addPerson(String name ); Person
I have read that in Java interfaces can't be instantiated ( in the documentation,

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.