I have a large scale C++ library written in C++ 98, which heavily uses C++ interface (precisely, C++ classes with only pure virtual functions) for event handling. Now seeing that my code are compiled by C++11/14 compiler, I'm thinking if I can reduce boilerplate code by using C++11 lambda to replace interface implementation.
In my library, there are some C++ interfaces who have only a single method, for example, the following interface us used to define a simple task:
class SimpleTask
{
public:
void run();
};
My intention is to use C++ lambda to replace the old single-method interface implementation code like this:
void myFunction()
{
...
class MySimpleTask : SimpleTask //An inline class to implement the iterface
{
public:
void run()
{
//Do somthing for this task
...
delete this; //Finally, destroy the instance
}
};
MySimpleTask * myThreadTask = new MySimpleTask();
Thread myThread(L"MyTestingThread", myThreadTask);
myThread.start();
...
}
In Java 8, we can use Java lambda to implement a single-method interface to write code more concise than using anonymous class. I did a bit research in C++11 and found there nothing similar to this.
Since my library's event handling code is designed in object oriented pattern, not in functional coding style, is there way to use lambda to help reduce those single-method interface implementation code?
Aucun commentaire:
Enregistrer un commentaire