1

How can I do this in C++

interface ActionListener { void actionPerformed(); }
jButton.addActionListener(
    new ActionListener(){ void actionPerformed() { do something } }
);

So far I know this,

class ActionListener {
  public : 
    virtual ~ActionListener(){}
    virtual void actionPerformed() = 0;
}

After this what do I do ... any keywords to do this kind of implemention would also help.

Aleksandr Kravets
  • 5,360
  • 7
  • 50
  • 66
Jeffrey Chen
  • 237
  • 1
  • 3
  • 16
  • 1
    Is this what you want? http://stackoverflow.com/questions/318064/how-do-you-declare-an-interface-in-c – Mysticial Jul 12 '12 at 05:14
  • I read that before but I don't know how to do the above java concept in c++ , i.e create a pointer to the interface and define its method... i've seen this implemented in java in netbeans to simplify readability of code for actionPerformed on buttons – Jeffrey Chen Jul 12 '12 at 05:17

4 Answers4

4

C++ doesn't really have anonymous classes like you find in Java. The usual thing is just to declare a subclass that inherits from the interface class. The closest you come to Java's anonymous class instance is something like this:

class : public ActionListener {
public:
    virtual void actionPerformed() {
        // do something
    }
} listener;
thing.addActionListener( listener );
Ted Hopp
  • 222,293
  • 47
  • 371
  • 489
  • @JeffreyChen - It is known by various names: nameless class; anonymous class; unnamed class. It's a standard part of C++. Just google those terms to get lots of links. See, for instance, the [MSDN](http://msdn.microsoft.com/en-us/library/a3bbz53t%28v=vs.80%29.aspx) description. – Ted Hopp Jul 12 '12 at 05:44
1

There isn't much else to do in C++. You may want to use virtual inheritance when you implement, to avoid the diamond inheritance problem in case you eventually use multiple inheritance:

class SomeActionListener : virtual public ActionListener{
  public : 
    virtual ~ActionListener();
    virtual void actionPerformed();
}

In C++11, you can also use override for the method implementation. This checks at compile time in case you inadvertently mistyped the method name or got the cv qualifiers wrong, in which case you wouldn't be overriding:

class SomeActionListener : virtual public ActionListener{
  public : 
    virtual ~ActionListener();
    virtual void actionPerformed() override;
}
juanchopanza
  • 210,243
  • 27
  • 363
  • 452
1

"How can I do this in C++?" -- It looks that what you are trying to do is to register a callback in a button. In C++, you do it in a different way: you do not need to provide any abstract class or an interface (in OO sense). Instead your button is likely to provide the following function for registering a callback:

void Button::addActionListener(std::function<void()> callback);

The type of callback is "any function, function pointer, or reference that can be called with zero arguments and returns nothing". Then you register your callback like this:

cppButton.addActionListener( [&]{ do something; } );
// lambda
Andrzej
  • 4,435
  • 23
  • 32
0

Inherit from ActionListener.

C++ supports multiple inheritance. Java does not, so it must rely on interfaces.

Adam
  • 15,548
  • 4
  • 47
  • 89
  • I know this class myClass: public ActionListener { void actionPerformed(){} }; but is there a way not to inherit from ActionListener, like in java myClass would not implement actionListener because i can create an object of ActionListener and redeifne actionPerformed() – Jeffrey Chen Jul 12 '12 at 05:20