Using std :: function in a member initialization list

I have a typedef:

typedef S32(iMyDataClass1::*getDataFunction_t)(void);

and type:

struct functionMap_t {
    std::vector<getDataFunction_t> pDataFunctionTable;
    struct dataRequestor_t dataRequestor;
};

Then I have a member variable:

functionMap_t FnMap1;

What then did I configure in the list of member initializers:

myClass::myClass() : FnMap1({ 
 {
     &iMyDataClass1::getData1,
     &iMyDataClass1::getData2,
     &iMyDataClass1::getData3
   },
   dataRequestor1
 })

What I then use in the constructor:

addNewFnMap(FnMap1);

It all works great. Unfortunately, it does not expand for function pointers to different classes, since it is getDataFunction_tbound to pointers iniMyDataClass1

I tried using templates, but ran into problems that I could not get around. Now I am trying to use std::function, which means that I am changing the original typedef to (I think):

typedef std::function<S32(void)> getDataFunction_t;

What is the correct way to configure the initializer list in this situation? Use here std::bind?

: . , , , :

{ 
  {
    (std::bind(&iMyDataClass1::getData1, functionToFetchCorrectObject())),
    (std::bind(&iMyDataClass1::getData2, functionToFetchCorrectObject())),
    (std::bind(&iMyDataClass1::getData3, functionToFetchCorrectObject()))
  },
  PGN65370
}
+4
2

-. , std::function<S32(void)>, .

myClass::myClass() : FnMap1({
  {
     [this](){ return getData1(); },
     [this](){ return getData2(); },
     [this](){ return getData3(); },
  },
  dataRequestor1
})

, , this . , , vector. reinterpret_cast , , , , ?

+3

, , :

struct functionMap_t {
  std::vector<std::function<S32(void)>> pDataFunctionTable;
  struct dataRequestor_t dataRequestor;
};

, , , , , std:: functions .

std::function<S32(void)> iMyDataClass1::getData1Getter() {
  auto f = [this] () {return this->getData1()};
  return f;
}

:

iMyDataClass1 o;
...

FnMap1({o.getData1Getter(), ..., dataRequestor1});

, , . . : , , (, ). , , .

EDIT: - Observer ++ 11. - , , , .

+2

All Articles