1. Help make a list of f-tions.
  2. Will the defaults work?

And if there are tips on how to remember, I will be grateful)

class A { public: A(); void f0(bool b = true); void f1(bool b = true); void f2(bool b = true); std::list<???> listFunc; // тут нужна помощь //QList<???> listFunc; } A::A() { listFunc.append(f0); listFunc.append(f1); listFunc.append(f2); } void main() { A a; a.listFunc.at(1)(false);// или a.listFunc[1](false); a.listFunc.at(0)(); } 
  • These are not ordinary functions, but class methods. For which object do you want to call these f0 , f1 , f2 ? - AnT 3:32 pm
  • one
    Non-static class functions belong to an instance of a class. They are called for the object - AR Hovsepyan
  • for this object. Moreover, this object will be in a single copy. - Kto To

2 answers 2

Your "functions" are not just functions, but class methods. It is not clear from your question for which object you are going to call these class methods.

for example

 std::list<std::function<void(bool)>> listFunc; 

and further

 listFunc.push_back(std::bind(&A::f0, this, std::placeholders::_1)); listFunc.push_back(std::bind(&A::f1, this, std::placeholders::_1)); listFunc.push_back(std::bind(&A::f2, this, std::placeholders::_1)); 

or through lambda

 listFunc.push_back([this](bool b) { f0(b); }); listFunc.push_back([this](bool b) { f1(b); }); listFunc.push_back([this](bool b) { f2(b); }); 

Call

 listFunc.front()(false); 

In this embodiment, the binding of a specific object ( this ) is done at the time of filling the list.


You can do the same

 std::list<void (A::*)(bool)> listFunc; 

Further

 listFunc.push_back(&A::f0); listFunc.push_back(&A::f1); listFunc.push_back(&A::f2); 

But the challenge then will be

 (a.*listFunc.front())(false); 

In this embodiment, specifying a particular object ( a ) is done at the time of the call.


The use of std::function in the first version introduces unnecessary overhead, in many cases completely unjustified. But due to the fact that neither the type of std::bind , nor the type of lambda in C ++ is uniquely defined, one has to use std::function willy-nilly. (In this respect, the outdated std::bind1st was better.)

A more efficient alternative to the first option may be a "manual" implementation.

 struct Af { A *self; void (A::*f)(bool); void operator ()(bool b) const { (self->*f)(b); } }; 

and further

 std::list<Af> listFunc; ... listFunc.push_back({ this, &A::f0 }); listFunc.push_back({ this, &A::f1 }); 

and challenge

 listFunc.front()(false); 
     class A { public: A(); void f0(bool b = true); void f1(bool b = true); void f2(bool b = true); std::list<void (A::*)(bool)> listFunc; // ??? ????? ?????? //QList<???> listFunc; }; A::A() { listFunc.push_back(&A::f0); listFunc.push_back(&A::f1); listFunc.push_back(&A::f2); } int main() { A a; for(auto f: a.listFunc) (a.*f)(false);// ??? a.listFunc[1](false); } 

    Naturally, the default values ​​will not work, because their substitution is performed at compile time; your functions are called by address, and which function will be called — the compiler is unable to determine.

    • one
      Since the addition occurs in the class constructor, you can simply: listFunc.push_back (f0); ... - AR Hovsepyan
    • @ARHovsepyan Is it possible exactly? Hz believes that it is impossible: gcc.godbolt.org/z/7UQIl8 - HolyBlackCat pm
    • @HolyBlackCat, I don’t know for some reason what the compiler thinks is that the class constructor or member function cannot work with functions of the same class, without pointers to class members. The same object, the same scope. My compiler takes it normally, and I read about it for a long time, I don’t remember which one, but in the Straustrup book ... But, in any case, thanks, I’m wondering why somewhere this could be a mistake - AR Hovsepyan
    • 2
      @HolyBlackCat, I am 54 years old, and I knew only letters from 51, but in programming only about bit operations .. It’s hard to work and do family affairs, communicate with grandchildren, children and other people, learn programming independently and well learn english well. So I can not understand what is written there, but thanks ... - AR Hovsepyan
    • one
      @ARHovsepyan And, yes, I did not notice. The -pedantic key (or something like that, in a word, strict compliance with the standard — I don’t work with GCC, not sure how correctly) didn’t try? - Harry