mardi 30 juin 2020

c++ class using a lambda in constructor

Using c++11 I want to create a class that uses a lambda as part of a calculation.

//contrived sample of potential usage
void random_class::some_function(void)
{
 auto an_object = new my_custom_object(5, [this](){ return random_class_member * 5; });
 an_object.do_some_processing();
 random_class_member++;
 an_object.do_some_processing();
}

I am not quite sure how to go about declaring and defining my_custom_object.

class my_custom_object
{
public:
 template <typename Proc>
 my_custom_object(int a, Proc p)
 {
  privatea = a;
  privatep = p;
 }
 void do_some_processing()
 {
  privatea += privatep();
 }
private:
 int privatea;
 Proc privatep;
}

unknown type name 'Proc'

Aucun commentaire:

Enregistrer un commentaire