0

I'm making a class Button; which constructor is getting a pointer to function, to execute on click. Is there any easy way to define the function I want to give it? Like without name.. for example when I'm creating the button, just to open the scopes and write the function without special name. Because I have to create many buttons and I don't want to write a function for each button and with it's own name..(names collision etc)

like for example this java code:

someObject.addMyListener(new MyListener() {
  public void processEvent(MyEvent e) {
    // How do you access the variable outerClassField?
  }
});

maybe I'm wrong but we writing the MyListener function ON PLACE without any special name or pre define.

Is it possible in c++?

6
  • 1
    Are you using c++0x? And as a general comment to all your questions - don't try solving problems Java-way when you're using C++. You have severe design issues, that's why you're getting stuck all the time. Commented Oct 24, 2011 at 21:11
  • @littleadv no idea what is this.. using visual studio 2010 express. what kind of design issues? Commented Oct 24, 2011 at 21:23
  • @Valdp - here's a link to the c++0x compiler support matrix. MSVC10 should support lambda's, according to the matrix. wiki.apache.org/stdcxx/C%2B%2B0xCompilerSupport Commented Oct 24, 2011 at 21:42
  • 2
    @Vladp: the design issue is that you are attempting to use approaches from one language (Java) to solve problems in another (C++). The languages are different and have different best practices. Commented Oct 24, 2011 at 21:42
  • @Evan Teran and littleadv you are both wrong. First I'm thinking of what I need and then I remembered that there is something similar in java so I wrote that example for YOU to understand what I mean.. And If you think you know my way of thinking, then good luck :D!! BTW THANKS FOR THE VOTE DOWN WHOEVER IT WAS! Commented Oct 24, 2011 at 21:55

4 Answers 4

3

C++11 introduces lambdas, which are anonymous functions capable of capturing variables.

See our FAQ: What is a lambda expression in C++11?

Sign up to request clarification or add additional context in comments.

1 Comment

It's a recently standardised new version of C++, with lots of rather handy features. Quite a few compilers have partial support for the new version now.
1

Have a virtual function in the Button class and override it in a derived class. For each type of Button you want to make, create a new class derived from Button with its own implementation of the OnClick function.

This is the whole point of polymorphism in an object-oriented language.

8 Comments

I'm surely don't want to write a WHOLE CLASS for each button.. I'm creating button objects which contains a pointer to function..
@vladp WHOLE CLASS would be just that function. You have to write it anyway, so what's the problem?
@Vladp I think you should have a look at the FAQ on Lambda expressions provided by Ben Voigt.
Are you creating a distinct function for each button? Do you need a constructor/destructor for them?
@David Schwartz I want to. of the button yes but its unique to the button class..
|
1

Use std::function< void() > (or the Boost implementation if your compiler does not include TR1 yet). It can be set to anything that can be invoked using thing().

Comments

0

Java style anonymous classes (which is what you're doing in your example) may be similar to lambda functions, but only worth looking into if you're using the latest C++ standard (google "C++11").

If you're not using C++11, then you can instead pass a pointer to a function that is not defined in a class or is a static method. Never pass a pointer to a non-static method as that can cause some serious headaches if you're not careful.

But generally speaking, you don't want to do any of that either. Like others have already stated, just make a class and pass a pointer to that instead. This is essentially what you're doing in your Java example, except you're doing it inline.

The nice thing about C++ is that you can define more then one class in a source file and you can keep it out of the header file if you don't want it to be part of the public interface.

1 Comment

My idea was creating a special file with all the functions, and when creating a button just pass it the pointer to one of them, it's not a big deal except that I have to manage them carefully.. I just hoped that maybe I can write them inline and not as a whole new function. Static function can only be in class right?(because I'm gonna write it outside and it's probably already static)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.