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++?

Vladp
  • 4,003
  • 3
  • 30
  • 66
  • 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. – littleadv Oct 24 '11 at 21:11
  • @littleadv no idea what is this.. using visual studio 2010 express. what kind of design issues? – Vladp Oct 24 '11 at 21:23
  • @Valdp - here's a link to the c++0x compiler support matrix. MSVC10 should support lambda's, according to the matrix. http://wiki.apache.org/stdcxx/C%2B%2B0xCompilerSupport – littleadv Oct 24 '11 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. – Evan Teran Oct 24 '11 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! – Vladp Oct 24 '11 at 21:55
  • @Vladp relax with the attitude, man. תרגיע. People are trying to help you here. – littleadv Oct 24 '11 at 22:03

4 Answers4

3

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

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

Community
  • 1
  • 1
Ben Voigt
  • 260,885
  • 36
  • 380
  • 671
  • 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. – Flexo Oct 24 '11 at 21:41
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.

David Schwartz
  • 166,415
  • 16
  • 184
  • 259
  • 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 Oct 24 '11 at 21:18
  • @vladp WHOLE CLASS would be just that function. You have to write it anyway, so what's the problem? – littleadv Oct 24 '11 at 21:21
  • @Vladp I think you should have a look at the FAQ on Lambda expressions provided by Ben Voigt. – Chad Harrison Oct 24 '11 at 21:22
  • Are you creating a distinct function for each button? Do you need a constructor/destructor for them? – David Schwartz Oct 24 '11 at 21:23
  • @David Schwartz I want to. of the button yes but its unique to the button class.. – Vladp Oct 24 '11 at 21:28
  • @littleadv it was suppose to mean he didn't read my question.. that's all. It would work and I would do it if there was such syntax as in java(it's about the same what in the java example) – Vladp Oct 24 '11 at 21:29
  • There's nothing wrong with creating a class just to implement a virtual function in a particular way. – David Schwartz Oct 24 '11 at 21:29
  • @David Schwartz you are off-topic. read my question again or tell me what is not clear.. this is even good BUT if i will have 100 buttons it would be crazy to manage button1, button2, button3... but if it would be a part of code without special name(like lambdas) it would be a lot easier. – Vladp Oct 24 '11 at 21:33
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().

K-ballo
  • 76,488
  • 19
  • 144
  • 164
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.

Evan Teran
  • 80,654
  • 26
  • 169
  • 231
Glaucus
  • 548
  • 3
  • 13
  • 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) – Vladp Oct 24 '11 at 21:49